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