设置List的属性,它也是List和另一个linq查询

时间:2013-08-26 15:20:30

标签: c# asp.net performance linq list

我有以下示例代码。我想加入一个用户的所有孩子。 UserInfo和ChildInfo之间存在一对多关系。我已经尝试使用嵌套选择,但花了很多时间来查询大量数据。我需要返回加入ChildInfo的所有UserInfo。对此有简单的linq解决方案吗?

public class UserInfo 
{
    private int _userId;
    public int UserId
    {
        get { return _userId; }
        set { _userId = value; }
    }
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }       
    private List<ChildInfo> _childInfo;
    public List<ChildInfo> ChildInfo
    {
        get { return _childInfo; }
        set { _childInfo = value; }
    }
}

public class ChildInfo
{
    private int _childId;
    public int ChildID
    {
        get { return _childId; }
        set { _childId = value; }
    }       
    private int _userId;
    public int UserId
    {
        get { return _userId; }
        set { _userId = value; }
    }
    private string _childName;
    public string ChildName
    {
        get { return _childName; }
        set { _childName = value; }
    }
}

var userList = new List<UserInfo>();      
userList = (from db in context.UserInfos                                                                        
    select new UserInfo ()
    {
        UserId = db.ID,
        Name = db.Name
    }).ToList();

var childList = new List<ChildInfo>();
childList = (from child in context.Children
          join user in userList on child.UserID equals cus.ID
          select new ChildInfo()
        {
            ChildID = db.ID,
            UserId = db.UserId,
            ChildName = db.Name    
        }).ToList();

4 个答案:

答案 0 :(得分:1)

您可以尝试直接加入表格而不是水合结果:

from child in context.Children
join user in context.UserInfos 
    on child.userID equals user.ID
select new ChildInfo()
    {
        ChildID = child.ID,
        UserId = user.ID,
        ChildName = child.Name    
    }).ToList();

答案 1 :(得分:0)

您当前的查询将在数据库上执行两次,一次用于获取用户列表,稍后获取ChildInfo,您可以使用以下连接执行一个查询:

childList = (from child in context.Children
          join user in context.UserInfos on child.ID equals user.ID
          select new ChildInfo()
        {
            ChildID = db.ID,
            UserId = db.UserId,
            ChildName = db.Name    
        }).ToList();

答案 2 :(得分:0)

根据我在查询中看到的内容,由于Child.ID = User.ID,最后一个结果中的所有行都有join condition。但您仍想同时选择Child.IDUser.ID?实际上你的查询等于这个:

var childList = from child in context.Children
                where context.UserInfos.Any(x=>x.ID == child.ID)
                select new ChildInfo(){
                           ChildID = child.ID,
                           UserID = child.ID,
                           ChildName = child.Name
                       };

答案 3 :(得分:0)

我相信你想要的是:

userList = (from user in context.UserInfos                                                                        
select new UserInfo ()
{
    UserId = user.ID,
    Name = user.Name
    ChildInfo = (from child in context.ChildInfo where child.UserId == user.Id select
    new ChildInfo
    {
        ChildID = child.ID,
        UserId = user.Id,
        Name = child.Name
    }).ToList()
}).ToList();

如果速度太慢,您可能会错过UserId列上的索引。