提供的Linq声明旨在仅收回有孩子的父母的数据,这些孩子已经读过这本书" Huck ........."。出于某种原因,我的查询似乎忽略了Where
子句,因为我每次都返回整个数据集Parents
。这个查询在哪里失败了?为什么Where
似乎不会影响结果?
List<Parent> we = Parents.Select(par => new Parent
{
ParentID = par.ParentID,
Children = parents.SelectMany(parData => parData.Children).ToList().Where(child => new Child
{
ChildID = child.ChildID,
Books = child.Books
}.Books.Contains("Huck........")).ToList()
}).ToList();
public class Parent
{
public Parent() { }
public int ParentID { get { return _parentID; } set { _parentID = value; } }
public List<Child> Children { get { return _child; } set { _child = value; } }
}
public class Child
{
private int _childID;
private List<string> _targets;
public Child() { }
public int ChildID { get { return _childID; } set { _childID = value; } }
public List<string> Books { get { return _books; } set { _books = value; } }
}
Parent ID | Parent | Child ID | Child | Books
1 | John | 4 | Suzy | Grapes of...
1 | John | 4 | Suzy | Huck........
1 | John | 5 | James | The adven...
2 | Sally | 4 | Suzy | Grapes of...
2 | Sally | 4 | Suzy | Huck........
2 | Sally | 5 | James | The adven...
3 | Fred | 6 | Jane | Purple......
答案 0 :(得分:3)
因此...
.Where(child => new Child
{
ChildID = child.ChildID,
Books = child.Books
}.Books.Contains(Convert.ToInt32("Huck........"))
可以缩短为
.Where(child => child.Books.Contains(Convert.ToInt32("Huck........"))
但是,调用Convert.ToInt32("Huck........")
并不会向您提供标题为"Huck........"
的图书的ID。它会将字符串转换为一个有些无用的数字。