Incorrect results, what am I doing wrong?

I have the following simple LINQ to SQL statement:

string strUserID = Eval("whoInsert").ToString();

insLusHmoobDataContext userDataContext = new insLusHmoobDataContext();

var userName = from usr in userDataContext.Users
               where usr.UserId.Equals(strUserID)       
               select usr.UserName;

return userName.ToString();

Instead of showing me UserNamefrom a table aspnet_Users, he showed me an SQL select statement. Any idea?

+3
source share
3 answers
var userName = (from usr in userDataContext.Users where usr.UserId.Equals(strUserID)     
select usr.UserName).SingleOrDefault();

Suppose there will be one user for this identifier, otherwise you can use FirstOrDefault().

+1
source

You need to add .Single (). ToString ();

Otherwise, you have not actually fulfilled the request - you yourself answered the request itself.

+4
source

userName - LINQ to SQL, SQL- .

, , .First() ToString();

, . FirstOrDefault() , :)

+2
source

Source: https://habr.com/ru/post/1774149/


All Articles