Linq不需要的清单

A191919

以下代码产生两个项目。第一项包含Component对象列表第二项还包含Component对象列表在此示例中,第二个列表应该为空,因为该Component列表没有DishID=10我该如何解决这个问题?

换句话说result[1].components.Count应为0。

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Component> Component = new List<Component>();
            List<Dish> Dish = new List<Dish>();
            Dish.Add(new Dish { DishID = 9, CategoryID = 6, DishName = "Pork" });
            Dish.Add(new Dish { DishID = 10, CategoryID = 6, DishName = "Beef" });
            Component.Add(new Component { ComponentID = 1, DishID = 9, AmountID = "1", NameID = "1" });

            List<Item> result = (from dishes in Dish
                                 join components in Component on dishes.DishID equals components.DishID
                                     into item
                                 from p in item.DefaultIfEmpty()
                                 select new { CategoryID = dishes.CategoryID, DishID = dishes.DishID, ComponentID = p != null ? p.ComponentID : default(int), DishName = dishes.DishName, AmountID = p != null ? p.AmountID : null, NameID = p != null ? p.NameID : null }).ToList().GroupBy(key => key.DishID)
                                   .Select(g => new Item()
                                   {
                                       DishID = g.Key,
                                       components = g.Select(t => new Component { AmountID = t.AmountID, ComponentID = t.ComponentID, DishID = t.DishID, NameID = t.NameID }).ToList()
                                   })
                                       .ToList();
        }
    }
    public class Item
    {
        public int DishID { get; set; }

        public List<Component> components { get; set; }
    }

    public partial class Component
    {
        public int ComponentID { get; set; }
        public int DishID { get; set; }
        public string AmountID { get; set; }
        public string NameID { get; set; }
    }

    public partial class Dish
    {
        public int DishID { get; set; }
        public int CategoryID { get; set; }
        public string DishName { get; set; }
    }
}
肯廷克

如果ComponentId为空(在这种情况下为0),则不存在匹配项,因此Component在创建结果列表项时过滤空对象,在这种情况下.Where (x => x.ComponentID > 0),请在最后一个select语句之前使用

        List<Item> result = (from dishes in Dish
                             join components in Component on dishes.DishID equals components.DishID into item
                             from p in item.DefaultIfEmpty()
                             select new 
                                { 
                                    CategoryID = dishes.CategoryID, 
                                    DishID = dishes.DishID, 
                                    ComponentID = p != null ? p.ComponentID : default(int), 
                                    DishName = dishes.DishName,
                                    AmountID = p != null ? p.AmountID : null, 
                                    NameID = p != null ? p.NameID : null 
                                })
                               .ToList()
                               .GroupBy(key => key.DishID)
                               .Select(g => new Item()
                               {
                                   DishID = g.Key,
                                   components = g
                                                .Where (x => x.ComponentID > 0)
                                                .Select(t => new Component 
                                                                { 
                                                                    AmountID = t.AmountID, 
                                                                    ComponentID = t.ComponentID, 
                                                                    DishID = t.DishID,
                                                                    NameID = t.NameID 
                                                                })
                                                  .ToList()
                               })
                               .ToList();

现在,结果包含两个项目,第一个具有DishId=9,第二个列表(用于DishId=10)为空。

如果我正确理解您的要求,我认为代码可以简化:

  • 遍历每个菜品
  • 从组件列表中为当前选定的DishId选择项目:如果不匹配,则列表为空

代码如下:

        List<Item> result1 = new List<Item>();
        Dish.ForEach(dish =>
        {
            var item = new Item
            {
                DishID = dish.DishID,
                components = Component.Where (c => c.DishID == dish.DishID).ToList()
            };
            result1.Add(item);
        });

输出与您的代码相同。


由于您正在LEFT JOIN使用LINQ(带有from p in item.DefaultIfEmpty()),因此我假设您还希望从结果中排除空项目。这可以通过在第一个查询中添加where条件(where ((p != null) && (p.ComponentID > 0)))来完成:

       List<Item> result = (from dishes in Dish
                             join components in Component on dishes.DishID equals components.DishID into item
                             from p in item.DefaultIfEmpty()
                             where ((p != null) && (p.ComponentID > 0))
                             select new 
                                { 
                                    CategoryID = dishes.CategoryID, 
                                    DishID = dishes.DishID, 
                                    ComponentID = p != null ? p.ComponentID : default(int), 
                                    DishName = dishes.DishName,
                                    AmountID = p != null ? p.AmountID : null, 
                                    NameID = p != null ? p.NameID : null 
                                })
                               .ToList()
                               .GroupBy(key => key.DishID)
                               .Select(g => new Item()
                               {
                                   DishID = g.Key,
                                   components = g.Select(t => new Component 
                                                                { 
                                                                    AmountID = t.AmountID, 
                                                                    ComponentID = t.ComponentID, 
                                                                    DishID = t.DishID,
                                                                    NameID = t.NameID 
                                                                })
                                                  .ToList()
                               })
                               .ToList();

结果与预期的一样,仅包含的Component项目DishId=9

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章