views:

112

answers:

3

I am trying to return the Actual Value in this query but I only get the Expression. Can someone point me in the right direction please.

    public static String NurseName(Guid? userID)
    {
        var nurseName = from demographic in context.tblDemographics
                        where demographic.UserID == userID
                        select new {FullName = demographic.FirstName +" " + demographic.LastName};

        String nurseFullName = nurseName.ToString();

        return nurseFullName;
    }

nurseFullName ends up as --> SELECT ([t0].[FirstName] + @p1) + [t0].[LastName] AS [FullName] FROM [dbo].[tblDemographics] AS [t0] WHERE ([t0].[UserID]) = @p0

+3  A: 

Try this:

var nurseName = (from demographic in context.tblDemographics
                where demographic.UserID == userID
                select new {FullName = demographic.FirstName +" " + demographic.LastName}).First().FullName;

Your code was grabbing a collection, but not a specific item. This modification takes the first item in the list and returns the FullName property.

John Fisher
Note that .First will throw an exception if the item isn't found , which you might want so something unexpected doesn't go silent. Also the difference between the First and Single, is that the later throws an exception if there were more than a single row, which again is something you might want so the unexpected (maybe because of an error on the query, or something went wrong in the data) doesn't go silent.
eglasius
+3  A: 
public static String NurseName(Guid? userID)
    {
        var nurseName = from demographic in context.tblDemographics
                        where demographic.UserID == userID
                        select demographic.FirstName +" " + demographic.LastName;

        return nurseName.SingleOrDefault();
    }

In the above nurseName has the IQueryable expression, so it hasn't executed anything. Its when you enumerate on it, that the query is called. That's also the case when you call methods like SingleOrDefault.


If you were to use your original query expression instead, you can:

public static String NurseName(Guid? userID)
    {
        var query= from demographic in context.tblDemographics
                        where demographic.UserID == userID
                        select new {FullName = demographic.FirstName +" " + demographic.LastName; }
        var nurseName = query.SingleOrDefault();
        return nurseName != null ? nurseName.FullName : "";
    }


Difference between Single vs. SingleOrDefault is that the later allows an empty result. Difference between First vs. Single is that the later prevents more than 1 row.

eglasius
says `cannot convert expression type {FullName:String} to return type String`...
Refracted Paladin
@Refracted Paladin, notice I changed the query expression.
eglasius
The type returned by SingleOrDefault varies depending on what you declare in your select, in your version it will be an Anonymous type with a property FullName - in mine it will be string directly.
eglasius
Thank you. Not only did this fix it but you also taught me something about LINQ. Thanks for being so active on this question!
Refracted Paladin
Of course you will use String.Empty instead of ""
Bryan
+1  A: 
public static String NurseName(Guid? userID)
{
    var nurseName = (from demographic in context.tblDemographics
                    where demographic.UserID == userID
                    select new {FullName = demographic.FirstName +" " + demographic.LastName}).SingleOrDefault();

    if(null == nurseName)
    {
        //TODO: Uh-Oh ID not found...
    }

    string nurseFullName = nurseName.FullName;

    return nurseFullName;
}
Quintin Robinson