linq.没有列表

fre*_*hie -3 c# linq

我有一个看起来像这样的表:

UserID | FruitID
  4    |    34
  4    |   4355
  4    |   652
  5    |   5677
  5    |    562
  4    |    562
Run Code Online (Sandbox Code Playgroud)

现在,我正在传递一个UserID和一个像这样的FruitID:

var IsAuthorized = MyDC.FruitTable
                       .Any(f => f.UserID == TheUserID && 
                                 f.FruitID == TheFruitID);
Run Code Online (Sandbox Code Playgroud)

这返回一个布尔值.现在我想为FruitID列表编写相同的东西:我传入一个UserID和几个FruitIDs,我想要一个布尔表示"ALL the FruitIDs have a UserID == to the TheUserID".请注意,如果列表只包含一个与FruitID == f.FruitID && f.UserID == UserID不匹配的元素,则整个列表的返回值应为false.

如何重写我的查询以获取FruitID列表.

谢谢你的帮助.

Jon*_*eet 5

留下你的示例代码无法编译的事实,我怀疑你想要的东西如下:

// Here fruitIDs is a List<int> for the "target" fruit IDs
var isAuthorized = MyDC.FruitTable
                       .Where(u => fruitIDs.Contains(u.FruitID))
                       .All(u => u.UserID == TheUserID);
Run Code Online (Sandbox Code Playgroud)

这将检查每个条目,使得水果ID在给定列表中与指定的用户ID匹配.当然,还有其他的写作方式.

不会检查原始集合中的每个条目是否都有有效的水果ID.从问题是否是目的还不清楚......

  • @frenchie:这不是你在问题中写的......基本上很难理解*确切*你想要什么.如果您提供样本输入和预期输出,那么理解起来会更加简单. (2认同)