none
Why selecting a member first name not successful? RRS feed

  • Question

  • Here is my LINQ to SQL selection from the classNotesMembers table. I'm trying to grab the first name.

    string strFstName = (from fstName in album.classNotesMembers where fstName.userID == crntUserID select fstName.fstName).ToString();

    For some reason, the above code only give me this:

    SELECT [t0].[fstName]
    FROM [dbo].[classNotesMembers] AS [t0]
    WHERE [t0].[userID] = @p0

    Instead of giving me the result. Any suggestion is appreciated.
    Tuesday, November 10, 2009 4:38 PM

Answers

  • Hi 2Chucky,

    You are too close but missed a small point. LINQ queries are materialized using ToList() if they are expected to return more than one records and using First(), Single(), Last(), FirstOrDefault() etc if you want to get a single value.
    Calling ToString() to a LINQ Query will return the resultant TSQL query as you are experiencing in this situation.

    So you need to use something like:

    string strFstName =
    (from fstName in album.ClassNotesMembers
    where fstName.userID == crntUserID
    select fstName.fstName).FirstOrDefault()

    You may also want to have a look at the difference between these First(), Single(), etc in this MSDN document: http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods.aspx

    Regards,
    Syed Mehroz Alam
    My Blog | My Articles
    Tuesday, November 10, 2009 5:47 PM