以下代码产生两个项目。第一项包含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
)为空。
如果我正确理解您的要求,我认为代码可以简化:
代码如下:
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] 删除。
我来说两句