开发者

How to set C# Property with Linq Statement?

开发者 https://www.devze.com 2023-04-12 01:41 出处:网络
0 Project. I have property public string UserName { get; set; } can i set this property to retrieve user name using 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 开发者_StackOverflowu.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;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消