Lambda - 选择符合所有选定值的所有项目,例如的CheckBoxList

时间:2017-08-16 18:31:01

标签: linq model-view-controller lambda

我有一个类别列表 - 在复选框列表中循环显示。我有一些属于一个或多个类别的产品。如果用户选择3个类别,我需要一个lambda表达式,它可以仅输出符合所有选定类别的产品 - 而不是仅满足一个或两个类别的产品。

假设我有以下产品:

  • A ...属于类别1,2和4
  • B ...属于第2类
  • C ...属于类别3和4
  • D ...属于第2类和第4类

如果用户在复选框列表中选择类别2和4 - 输出(通过lambda表达式)应仅产品A和D

我希望这是有道理的。它可能很容易,但我无法弄清楚 - 现在已经找了几个小时的解决方案。不成功。

UPDATE! 感谢Deepak Mishra(我的朋友需要 - 我努力提取代码示例,Deepak Mishra为我做了一个 - 非常感谢)我现在可以使用这个原理代码示例进行更新:

类别:

public class Products
{
    public string ProductName { get; set; }
    public int Category { get; set; }
}

虚拟数据:

List<Products> ProdList = new List<Products>();//code that add Product and Category in list
ProdList.Add(new Products() { ProductName = "A", Category = 1 });//A ... belonging category 1, 2 and 4
ProdList.Add(new Products() { ProductName = "A", Category = 2 });//A ... belonging category 1, 2 and 4
ProdList.Add(new Products() { ProductName = "A", Category = 4 });//A ... belonging category 1, 2 and 4
ProdList.Add(new Products() { ProductName = "B", Category = 2 });//B ... belonging category 2
ProdList.Add(new Products() { ProductName = "C", Category = 3 });//C ... belonging category 3 and 4
ProdList.Add(new Products() { ProductName = "C", Category = 4 });//C ... belonging category 3 and 4
ProdList.Add(new Products() { ProductName = "D", Category = 2 });//D ... belonging category 2 and 4
ProdList.Add(new Products() { ProductName = "D", Category = 4 });//D ... belonging category 2 and 4

List<int> userInput = new List<int>() { 2, 4 };//user select category 2 and 4 in the list of checkboxes

更新:

这个表达式可以完成这项工作 - 但遗憾的是它会返回所有类别为2或4的产品。而不是我想要的:只有属于2类和4类的产品:

var Products = ProdList.Where(p => userInput.Contains(p.Category)).ToList();

2 个答案:

答案 0 :(得分:3)

您的产品类为 -

public class Products
{
    public string ProductName { get; set; }
    public List<int> Category { get; set; }
}

下面的代码将插入虚拟数据 -

List<Products> ProdList = new List<Products>();//code that add Product and Category in list
ProdList.Add(new Products() { ProductName = "A", Category = new List<int>() { 1, 2, 4 } });//A ... belonging category 1, 2 and 4
ProdList.Add(new Products() { ProductName = "B", Category = new List<int>() { 2 } });//B ... belonging category 2
ProdList.Add(new Products() { ProductName = "C", Category = new List<int>() { 3, 4 } });//C ... belonging category 3 and 4
ProdList.Add(new Products() { ProductName = "D", Category = new List<int>() { 2, 4 } });//D ... belonging category 2 and 4
List<int> userInput = new List<int>() { 2, 4 };//user select category 2 and 4 in the list of checkboxes

选择产品或输出的最终landa表达式(通过lambda表达式) -

 var Products = ProdList.Where(p => userInput.All(c => p.Category.Contains(c))).Select(p => p.ProductName).ToList();

答案 1 :(得分:1)

随着您提出的问题得到更新。您需要使用以下代码 -

  

//来自产品清单的数据

  var prodG = ProdList.GroupBy(m => m.ProductName).Select(g => new
        {
            ProductName = g.Key,
            data = g.Select(prod => new
            {
                prod.ProductName,
                prod.Category
            })
        }).ToList();
  

//从分组产品列表中选择数据

  var Products = prodG.Where(pg => userInput.All(c => pg.data.Where(m => m.ProductName == pg.ProductName)
            .Select(m => m.Category)
            .ToList().Contains(c)))
            .Select(p => p.ProductName)
            .ToList();