如何返回一个bool,其中list只包含一个或多个所需的值

Cod*_*ght -1 c# linq

我正在努力弄清楚如何返回一个只包含一个或多个所需查询参数的项目列表.

我认为TrueForAll可能有效,但如果其中一个不存在则返回false.

var hasValidOptions = entity.clientcodes.TrueForAll(x => x.code == "B"
                                                      || x.code == "C"
                                                      || x.code == "E"
                                                      || x.code == "G"))
Run Code Online (Sandbox Code Playgroud)

以下是我正在尝试做的一些例子(我只关注B,C,E和G):

  • 清单1:A B E G- > false因为它包含A.
  • 清单2:B G- > true,因为它包含B和G.
  • 清单3:E- > true,因为它包含E.
  • 清单4:B C E G- > true,因为它包含B,C,E和G.

我怎样才能做到这一点?

The*_*der 6

我相信你可以使用All:

list.All(x => x.code == "B" || x.code == "C" || x.code == "E" || x.code == "G");
Run Code Online (Sandbox Code Playgroud)

您可以通过使用它们的数组来更轻松地修改选项,如:

string[] options = new [] { "B", "C", "E", "G" };
list.All(x => options.Contains(x.code));
Run Code Online (Sandbox Code Playgroud)