C# 选择数量字段最少相等的所有行-实体框架

C# 选择数量字段最少相等的所有行-实体框架,c#,entity-framework,linq,C#,Entity Framework,Linq,我有类似于ProductInventory的表格,因为我有一些有数量的产品 我希望选择所有行,其中字段中的最小值等于我的输入(数字) 我试着这样做: List<product> Products = new List<product> { new product{Id=1,Name="A",Quantity=1}, new product{Id=1,Name="A",Quantity=2}, new product{Id=1,Name="A",Q

我有类似于
ProductInventory
的表格,因为我有一些有数量的产品

我希望选择所有行,其中字段
中的最小值等于我的输入(数字)

我试着这样做:

List<product> Products = new List<product> { 
    new product{Id=1,Name="A",Quantity=1},
    new product{Id=1,Name="A",Quantity=2},
    new product{Id=1,Name="A",Quantity=3},
    new product{Id=1,Name="B",Quantity=4},
    new product{Id=1,Name="B",Quantity=7}
};

var result = Products
    .AsEnumerable()
    .GroupBy(r => r.Name)
    .Where(g => (int)g.Sum(r =>r.Quantity)<= 4)
    .ToList();
List Products=新列表{
新产品{Id=1,Name=“A”,数量=1},
新产品{Id=1,Name=“A”,数量=2},
新产品{Id=1,Name=“A”,数量=3},
新产品{Id=1,Name=“B”,数量=4},
新产品{Id=1,Name=“B”,数量=7}
};
var结果=产品
.可计算的()
.GroupBy(r=>r.Name)

其中(g=>(int)g.Sum(r=>r.Quantity)我不知道在linq中是否可能。但是你可以试试这个

var result = Products.AsEnumerable().Where(g => g.Name == "A").ToList();

int userInput =4;
var total = 0;
var selectList = new List<product>();
for (int i = 0; i < result.Count; i++)
{
   for (int j = i; j < result.Count; j++)
    {
      if (total + result[j].Quantity <= userInput)
      {
         total += result[j].Quantity;
         selectList.Add(result[j]);
       }
     }
     if (total == userInput)
       break;
     else
     {
        total = 0;
        selectList = new List<product>();
      }
}
if(userInput!=total)
 selectList = new List<product>();
var result=Products.AsEnumerable()。其中(g=>g.Name==“A”).ToList();
int userInput=4;
var合计=0;
var selectList=新列表();
for(int i=0;i如果(total+result[j].Quantity有了最新的更新,我想我终于明白你想做什么了

但是,这不起作用,因为您无法构建布尔数之和

var result = Products
    .AsEnumerable()
    .GroupBy(r => r.Name)
    .Where(g => g.Sum(r =>r.Quantity== 4))
    .ToList();
你真正想要的是

var result = Products
    .GroupBy(r => r.Name)
    .Where(g => g.Sum(r =>r.Quantity) >= 4) //or == 4 or whatever
    .ToList();

你的问题不清楚?你尝试了什么?如果是,分享代码我不知道我该怎么做,我也不尝试,因为我不知道
我在谷歌上搜索过,但找不到任何结果。
我非常怀疑……试试
LINQ where子句
,然后点击“我感觉很幸运”@ManfredRadlwimmer我的英语不是很好,我不知道应该搜索什么,严重混淆了你想做的事情请检查。你正在查找一个name=“A”的项目数量=4。但在你的列表中没有这样的项目。好的,我知道,但在fiddel中,返回两个recirds的总和为
quantity=4
,如果用户输入
4
,这应该返回所有行的总和为
quantity=4
,好的,亲爱的,这是错误的,请看,对不起,不要再试一次。没有任何项目的数量将为b您的代码还可以,谢谢,我在新的更新中更改了代码,我应该尝试使用您的代码