我正在努力弄清楚如何返回一个只包含一个或多个所需查询参数的项目列表.
我认为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):
A B E G- > false因为它包含A.B G- > true,因为它包含B和G.E- > true,因为它包含E.B C E G- > true,因为它包含B,C,E和G.我怎样才能做到这一点?
我相信你可以使用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)