Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# Linq群加入_C#_Linq - Fatal编程技术网

C# Linq群加入

C# Linq群加入,c#,linq,C#,Linq,如何从Recipe=中的Recipe编写说明。 我试图在d.DishID等于r.DishID的配方中使用连接r,但它给出了错误的结果。它会删除result1中的一项 导出的输出: 命名空间控制台应用程序3 { 班级计划 { 静态void Main(字符串[]参数) { 列表组件=新列表(); 列表盘=新列表(); 列表配方=新列表(); 添加(新菜{DishID=9,CategoryID=6,DishName=“Pork”}); 添加(新菜{DishID=10,CategoryID=6,Dis

如何从
Recipe=
中的
Recipe
编写
说明
。 我试图在d.DishID等于r.DishID的配方中使用
连接r,但它给出了错误的结果。它会删除result1中的一项

导出的输出:

命名空间控制台应用程序3
{
班级计划
{
静态void Main(字符串[]参数)
{
列表组件=新列表();
列表盘=新列表();
列表配方=新列表();
添加(新菜{DishID=9,CategoryID=6,DishName=“Pork”});
添加(新菜{DishID=10,CategoryID=6,DishName=“Beef”});
Add(新组件{ComponentID=1,DishID=9,AmountID=“1”,NameID=“1”});
添加(新配方{DishID=9,RecipeID=0,Description=“Test”});
var result1=(
从盘中取出
//在d.DishID上的配方中加入r.DishID等于r.DishID
在d.DishID上的组件中连接c.DishID等于c.DishID到项目中
选择新项目{DishID=d.DishID,components=items.ToList()recipe=}
).ToList();
}
}
公共类项目
{
public int DishID{get;set;}
公共字符串名称{get;set;}
公共字符串配方{get;set;}
公共列表组件{get;set;}
}
公共部分类组件
{
公共int组件ID{get;set;}
public int DishID{get;set;}
公共字符串AmountID{get;set;}
公共字符串NameID{get;set;}
}
公共小菜
{
public int DishID{get;set;}
public int CategoryID{get;set;}
公共字符串名称{get;set;}
}
公共部分类配方
{
public int RecipeID{get;set;}
public int DishID{get;set;}
公共字符串说明{get;set;}
}
}

您想进行左连接

要做到这一点,您需要在linq中做一些额外的工作,不确定为什么它不支持本机,但它不是

        var result1 = (
        from d in Dish
        join c in Component on d.DishID equals c.DishID into items
        join r in Recipe on d.DishID equals r.DishID into recipes
        select new Item { 

                     DishID = d.DishID, 

                     components = items.DefaultIfEmpty()
                                       .Where(a=>a!=null)
                                       .ToArray(), 

                     recipe = recipes.DefaultIfEmpty()
                                    .Where(a=>a!=null)
                                    .Select(a=>a.Description)
                                    .FirstOrDefault() 
        }).ToList(); 

您想进行左连接

要做到这一点,您需要在linq中做一些额外的工作,不确定为什么它不支持本机,但它不是

        var result1 = (
        from d in Dish
        join c in Component on d.DishID equals c.DishID into items
        join r in Recipe on d.DishID equals r.DishID into recipes
        select new Item { 

                     DishID = d.DishID, 

                     components = items.DefaultIfEmpty()
                                       .Where(a=>a!=null)
                                       .ToArray(), 

                     recipe = recipes.DefaultIfEmpty()
                                    .Where(a=>a!=null)
                                    .Select(a=>a.Description)
                                    .FirstOrDefault() 
        }).ToList(); 

但它给出了错误的结果。
它是什么?样本输入?预期输出?更正代码,使其运行,现在它没有根据您刚才所说的给出如何在linq中执行左连接的答案,但您的代码没有编译,显示当前正在运行的代码是Dish=>Recipe 1到1的关系?你的编辑让我confused@konkked,是的,我看到了令人困惑的事情。对答案进行了修改,使用的代码稍微少一点,回答您的问题,这样您就可以删除问题中的答案部分,非常确定答案不应该在那里
,但它给出了错误的结果。
这是什么?样本输入?预期输出?更正代码,使其运行,现在它没有根据您刚才所说的给出如何在linq中执行左连接的答案,但您的代码没有编译,显示当前正在运行的代码是Dish=>Recipe 1到1的关系?你的编辑让我confused@konkked,是的,我看到了令人困惑的事情。对答案进行了修改,使用了稍微少一点的代码并回答了您的问题,这样您就可以删除您在问题中输入的答案部分,非常确定不应该在其中。您应该在recipes.DefaultIfEmpty()中添加来自r的
,以再次展平配方。然后
recipe=r?.Description
@GertArnold通常用C#5回答,除非我确定此人可以使用C#6当然,继续,只是指出正确的(imo)方向。你应该在recipes.DefaultIfEmpty()中添加
from r以再次展平配方。然后
recipe=r?.Description
@GertArnold通常用C#5回答,除非我确定此人可以使用C#6当然,继续,只是指出正确的(imo)方向。