0

我有下面的示例代码。我想加入用户的所有孩子。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

4 回答 4

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();
于 2013-08-26T15:23:24.470 回答
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
                       };
于 2013-08-26T15:43:57.737 回答
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列上的索引。

于 2013-08-26T17:30:12.743 回答
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();
于 2013-08-26T15:25:04.173 回答