3

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;
Alex
  • 177
  • 4
  • 11

1 Answers1

6

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.

bendewey
  • 39,709
  • 13
  • 100
  • 125
  • Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[System.Int32]' to type 'System.IConvertible'. I am getting this than – Alex Feb 26 '10 at 00:49
  • The message states that you are calling FirstOrDefault on a List>, I would verify the type of UserCarId. Additionally, you don't need the Include statement on your query. That may be causing your problem. – bendewey Feb 26 '10 at 01:14