none
beginner linq question (trying to simplify) RRS feed

  • Question

  • I have a table Users that has a very simple UserName, Password, EmailAddress.  All I want to do is get a single user returned without having to iterate the return.

    Currently I can pull the user that is returned like this

    MyDBContext db = new MyDBContext();
    var authuser = from d in db.Users where d.UserName == Login1.UserName select d;

    foreach (var muser in authuser )
       
    Debug.WriteLine(muser.UserName);    // this should only fire once ever

    However, is there a way I can just return the user?  Something like this

    User user = from d in db.Users where d.UserName == Login1.UserName select d;

    The above gives me this error 

    "Cannot implicitly convert type 'System.Linq.IQueryable<User>' to 'User'. An explicit conversion exists (are you missing a cast?)"


    What am I missing?

    Monday, January 5, 2009 11:52 AM

Answers

  • Allen Anderson said:

    Hi KristoferA, thanks for the information.  I'm fairly new to linq though so I don't get the association of the two.  How would I use .Single or anything like that in my linq query and further how would I go about casting that to the entity?

    User user = (from d in db.Users where d.UserName == Login1.UserName select d).Single(); 

    ...in short, the query is an IQueryable / IQueryable<User>. So by wrapping in parenthesis you can call either one of those methods (whichever one suits your purposes) directly on it.

    If the query always return a single record, they behave the same. The difference between the methods comes in if it can return more than one record or less than one record. (the .Single... methods will throw an exception if >1 record while the .First... methods will just pick the first out of many.    Both ...OrDefault methods will return a null (or specified value) if the query return zero records).
    Monday, January 5, 2009 2:46 PM
    Answerer

All replies

  • Take a look at the .Single, .SingleOrDefault, .First, .FirstOrDefault methods on IQueryable:

    http://msdn.microsoft.com/en-us/library/system.linq.queryable_methods.aspx

    They all do what you want but with slight variations in behavior.
    Monday, January 5, 2009 12:05 PM
    Answerer
  • Hi KristoferA, thanks for the information.  I'm fairly new to linq though so I don't get the association of the two.  How would I use .Single or anything like that in my linq query and further how would I go about casting that to the entity?
    Monday, January 5, 2009 12:19 PM
  • Allen Anderson said:

    Hi KristoferA, thanks for the information.  I'm fairly new to linq though so I don't get the association of the two.  How would I use .Single or anything like that in my linq query and further how would I go about casting that to the entity?

    User user = (from d in db.Users where d.UserName == Login1.UserName select d).Single(); 

    ...in short, the query is an IQueryable / IQueryable<User>. So by wrapping in parenthesis you can call either one of those methods (whichever one suits your purposes) directly on it.

    If the query always return a single record, they behave the same. The difference between the methods comes in if it can return more than one record or less than one record. (the .Single... methods will throw an exception if >1 record while the .First... methods will just pick the first out of many.    Both ...OrDefault methods will return a null (or specified value) if the query return zero records).
    Monday, January 5, 2009 2:46 PM
    Answerer
  • That did the trick, thank you very much.  I still don't understand exactly what that 'did' though.  Does .single just force a collection to spit out the only member or something?
    Monday, January 5, 2009 9:57 PM
  •  
    Allen Anderson said:

    That did the trick, thank you very much.  I still don't understand exactly what that 'did' though.  Does .single just force a collection to spit out the only member or something?


    Yes, .Single runs the full query (for all matching records), ensures there is exactly one member in the resulting collection and returns it. If more or less than one member it throws an exception.

    .First will translate into a "top 1" sql query so if your query has the potential of returning a lot of data then .First or .FirstOrDefault may be a better choice.
    Tuesday, January 6, 2009 1:11 AM
    Answerer