简体   繁体   中英

How to set C# Property with Linq Statement?

0 Project.

I have property

public string UserName { get; set; }

can i set this property to retrieve user name using linq statement?

from u in context.Users
where u.UserID==session["UserID"]
select u.UserName

something like this

public string UserName { get value; 
set from u in context.Users
    where u.UserID==session["UserID"]
    select u.UserName; }

Try

public string UserName {
    get {
        return (from u in context.Users
                where u.UserID==session["UserID"]
                select u.UserName).SingleOrDefault(); 
    }
}

The get part of your code should look like this if you try to return the UserName

get
{
   var result = (from u in context.Users
where u.UserID==session["UserID"]
select u.UserName).FirstOrDefault();
return result;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM