简体   繁体   English

我可以将C#代码转换为Linq代码吗?

[英]Can I can convert this C# code into some Linq code?

i have the following string array and would like to convert the contents into a linq Where clause like the following. 我有以下string array并想将内容转换为linq Where子句,如下所示。

I have ... 我有 ...

var words[] = new string [] { "aaa", "bbb", "ccc", "ddd" };

And would like .... 并希望....

.Where(x => x.Word == "aaa" && x.Word == "bbb" && 
            x.Word == "ccc" && x.Word == "ddd")

Cheers :) 干杯:)

Update: Why && and not || 更新:为什么&&而不是||

This is actually for RavenDb and it's smart enough to convert that linq to a Lucene query with AND 's in there... 这实际上是针对RavenDb的,它足够聪明,可以将linq转换为其中包含AND的Lucene查询...

So i really need it to be && 所以我真的需要它成为&&

Update 2: More clarrification why I would like to use && and not || 更新2:更清楚地说明为什么我要使用&&而不是||

The Linq provider in RavenDB, when told to translate the linq to lucene, does the following.. 当被告知将linq转换为lucene时,RavenDB中的Linq提供程序将执行以下操作。

.Where(x => x.Word == "aaa" && x.Word == "bbb")

get's translated to 得到翻译成

Query:aaa AND Query:bbb

so please, i would really appreciate any help and not suggetions to use || 所以请,我真的很感谢您的帮助,而不是使用|| . This is not linq to objects or linq to ef , etc.. 这不是linq to objects linq to ef也不是linq to ef ,等等。

.Where(x => words.Contains(x.Word))

UPD :这对于问题的初始修订完全有效

If you want to use && then you're asking for all the conditions to be satisfied. 如果要使用&&则要求满足所有条件。 You could try the All operator: 您可以尝试使用All运算符:

.Where(item => words.All(word => word == item));

This seems extremely counterintuitive but is worth a try! 这似乎非常违反直觉,但值得尝试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM