开发者

linq query from more tables

开发者 https://www.devze.com 2022-12-12 12:21 出处:网络
public ActionResult myItems() { var dataContext = new RecordsDataContext(); MembershipUser myObject = Membership.GetUser();
public ActionResult myItems() {

        var dataContext = new RecordsDataContext();
        MembershipUser myObject = Membership.GetUser();         
        string CurrentUserName = myObject.UserName.ToString();    

       var u开发者_JAVA技巧ser = from i in dataContext.myUsers
                   where i.userName ==CurrentUserName
                   select i.id;

        var items=from j in dataContext.OtherUsers
                   where j.id_user==user   /*error:operator '==' cannot be aplied to operands of type 'int' and 'System.Linq.Iquerable<int>'*/
                   select j;



           return View(items);

    }

Please help me with this error


Actually user from the first query is not a single int result. You need to check if user.Count() is not 0 and you could use user.First() to retrieve first row result.

But I suggest to use join (something like that):

public ActionResult myItems() {
    var dataContext = new RecordsDataContext();
    var query = from i in dataContext.myItems
                join ou in dataContext.OtherUsers
                on i.id_user equals ou.id_user //check real reference, since I don't know
                join mu in dataContext.myUsers
                on ou.id_user equals mu.id_user
                where mu.username == Membership.GetUser().UserName.ToString()
                select i;
    return View(query);
}
0

精彩评论

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