Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表中搜索项目_C# - Fatal编程技术网

C# 在列表中搜索项目

C# 在列表中搜索项目,c#,C#,搜索项目时 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 = "

搜索项目时

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" }} },
};
使用

它没有返回任何结果

var query = from lst in prn  where(lst.RList.Contains(lstr) )  select lst;

这不起作用,因为您正在搜索ReceipeList的特定实例(通过引用)。因此,您的查询将只返回具有ReceipeList的特定lstr实例的值,而不是具有与您指定的值相同的值的ReceipeList

您可以通过在ReceipeList中重写,或将查询修改为:

 foreach (var v in query)
 {
            Console.Write(v.Name.ToString()+" ordered   :");

            foreach(ReceipeList lst in v.RList)
            {
                Console.Write(lst.NameofRecipie.ToString()+",");
            }
            Console.WriteLine();
 }

这是因为它使用()方法通过谓词搜索ReceipeList实例,而不是搜索特定的实例。

我不确定,但您也可以看看,这是否会有所帮助

 foreach (var v in query)
 {
            Console.Write(v.Name.ToString()+" ordered   :");

            foreach(ReceipeList lst in v.RList)
            {
                Console.Write(lst.NameofRecipie.ToString()+",");
            }
            Console.WriteLine();
 }
string lstr = "Coak";
var query = from lst in prn
            where lst.RList.Any(r => r.NameofRecipie == lstr)
            select lst;