user1784622
user1784622

Reputation: 639

LINQ not returning desired data

I'm trying to write a very simple method which will match userId & return the corresponding imagepath string. My code is something like this:

public string GetImagePath(int _ID)
{
 var imagepath=from s in context.Userinfoes
               where s.UserID==_ID
               select s.UserImage;
 return imagepath.ToString();
}

The problem is the linq is not loading the data, rather its loading a string like "system.data.object.objectquery1[system.string]"! I wrote similar methods many times before but getting this problem for the first time.

FYKI:

I'm using a 3 tier architecture & updated my edmx before doing this. I removed my reference & added them again to solve the problem but didn't work.

Please help.

Upvotes: 0

Views: 89

Answers (2)

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

Your problem is in return imagepath.ToString() imagepath is not a data. It is IQueriable. So ToString() returns "system.data.object.objectquery1[system.string]". Before this code you need to iterate your query. So try this:

public string GetImagePath(int _ID)
{
    var imagepath=from s in context.Userinfoes
           where s.UserID==_ID
           select s.UserImage;

    return imagepath.FirstOrDefault().ToString();
}

Upvotes: 3

NoPyGod
NoPyGod

Reputation: 5067

you need to use Single()/First() to return one item from the enumerable list

Upvotes: 2

Related Questions