开发者

Entity framework - ObjectQuery return int property and assign it to variable

开发者 https://www.devze.com 2022-12-21 13:01 出处:网络
I need to get UserCarId(int) from this query and assign to int type variable. int UserCarId; Entities ctx = new Entities();

I need to get UserCarId(int) from this query and assign to int type variable.

 int UserCarId;


 Entities ctx = new Entities();
                var query = from enq in ctx.UserCars.Include("aspnet_Users")
                          开发者_如何学编程  where enq.aspnet_Users.UserId == currentUserId
                            select enq.UserCarId ;
                UserCarId = query;


If you want to get a single record use the FirstOrDefault method

UserCarId = query.FirstOrDefault();

In the case of int the default value is 0, so if no record is found then 0 will be returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号