person[] prn =
new person[]
{
new person { Name = "Robert", RList =
{new ReceipeList { NameofRecipie = "Coak" },
new ReceipeList { NameofRecipie = "Pizza" } } },
new person { Name = "Rahim", RList =
{ new ReceipeList { NameofRecipie = "Coak" },
new ReceipeList { NameofRecipie = "OnionBread" }} },
};
搜索项目时
ReceipeList lstr = new ReceipeList();
lstr.NameofRecipie = "Coak";
使用
var query = from lst in prn where(lst.RList.Contains(lstr) ) select lst;
它没有返回任何结果。
foreach (var v in query)
{
Console.Write(v.Name.ToString()+" ordered :");
foreach(ReceipeList lst in v.RList)
{
Console.Write(lst.NameofRecipie.ToString()+",");
}
Console.WriteLine();
}
答案 0 :(得分:3)
这不起作用,因为您正在搜索ReceipeList的特定实例(通过引用)。因此,您的查询将只返回具有您的特定lstr实例ReceipeList的值,而不是具有与您指定的值相同的ReceipeList的值。
您可以通过覆盖ReceipeList中的Equals或将查询重新编写为:
来完成此工作。string lstr = "Coak";
var query = from lst in prn
where lst.RList.Any(r => r.NameofRecipie == lstr)
select lst;
这是有效的,因为它使用Any()方法通过谓词搜索ReceipeList实例,而不是搜索特定实例。
答案 1 :(得分:0)
我不确定是否,但您也可以查看this,可能会有所帮助吗?