In My LINQ Query I use Where Clauses to check whether a property exists within an array of values. I would like to update the query:
在我的LINQ查询中,我使用Where子句来检查属性是否存在于值数组中。我想更新查询:
string[] itemColour
string[] itemType
List<displayItem> displayItems = (from item in allItems
Where itemColour.Contains(item.colour)
Where itemType.Contains(item.type)
select new displayItem
......................etc...etc
So what I have is
所以我拥有的是
'Where my 'itemColour' array contains this 'item.colour' select it'
'我的'itemColour'数组包含此'item.colour'选择它'
'Where my 'itemType' array contains this 'item.type' select it'
'我的'itemType'数组包含此'item.type'选择它'
What I'm after is
我所追求的是
'Where my 'itemColour' array has an item that has 'item.colour' anywhere in it select it
'Where my 'itemType' array has an item that begins with 'item.type' select it
'我的'itemColour'数组中有一个项目在其中的任何位置都有'item.colour'选择它'我的'itemType'数组有一个以'item.type'开头的项目选择它
I'm aware of 'StartsWith' and 'EndsWith' but don't know how to use them with these arrays.
我知道'StartsWith'和'EndsWith',但不知道如何将它们与这些数组一起使用。
note: itemColour may contain an item that has the colour code 'RGW' I would want to be able to select this item based on any of the three letters
注意:itemColour可能包含一个颜色代码为“RGW”的项目我希望能够根据三个字母中的任何一个选择此项目
Update
更新
The suggested solution :
建议的解决方案:
var query = from item in allItems
where itemColour.Any(c => c.Contains(item.colour))
&& itemType.Any(t => t.StartsWith(item.type)
select ...;
has an issue. If I'm trying to bring back an item with the colour Code 'RB'. My Array may contain two items 'R' and 'B' the suggested query is only returning items with colour code either 'R' or 'B' not both.
有一个问题。如果我想要带回颜色代码'RB'的项目。我的数组可能包含两个项目“R”和“B”,建议的查询只返回颜色代码为“R”或“B”的项目,而不是两者。
I am using MVC 5, LINQ TO Entity, .NET 4.5
我正在使用MVC 5,LINQ TO Entity,.NET 4.5
Thanks
谢谢
2 个解决方案
#1
5
I suspect you're looking for Any
, which allows you to check whether any item in a sequence (the arrays in this case) matches a predicate:
我怀疑你正在寻找Any,它允许你检查一个序列中的任何项(在这种情况下是数组)是否与谓词匹配:
var query = from item in allItems
where itemColour.Any(c => c.Contains(item.colour))
&& itemType.Any(t => t.StartsWith(item.type)
select ...;
There are potential optimizations depending on how many colour entries you have and the exact semantics.
根据您拥有的颜色条目数量和确切的语义,可能会进行优化。
If you want to find items which match either colour or type, use ||
instead:
如果要查找与颜色或类型匹配的项目,请使用||代替:
var query = from item in allItems
where itemColour.Any(c => c.Contains(item.colour))
|| itemType.Any(t => t.StartsWith(item.type)
select ...;
EDIT: Your description isn't clear, but it's entirely possible that you really want:
编辑:您的描述不明确,但您完全有可能真正想要:
itemColour.Any(c => item.colour.Contains(c))
instead. Think logically about what you want to check, and apply it appropriately.
代替。从逻辑上思考您要检查的内容,并适当地应用它。
#2
2
Something like:
就像是:
from item in allItems
where itemColour.Any(x => x.Contains(item.colour))
where itemType.Any(x => x.StartsWith(item.type))
select new displayItem
#1
5
I suspect you're looking for Any
, which allows you to check whether any item in a sequence (the arrays in this case) matches a predicate:
我怀疑你正在寻找Any,它允许你检查一个序列中的任何项(在这种情况下是数组)是否与谓词匹配:
var query = from item in allItems
where itemColour.Any(c => c.Contains(item.colour))
&& itemType.Any(t => t.StartsWith(item.type)
select ...;
There are potential optimizations depending on how many colour entries you have and the exact semantics.
根据您拥有的颜色条目数量和确切的语义,可能会进行优化。
If you want to find items which match either colour or type, use ||
instead:
如果要查找与颜色或类型匹配的项目,请使用||代替:
var query = from item in allItems
where itemColour.Any(c => c.Contains(item.colour))
|| itemType.Any(t => t.StartsWith(item.type)
select ...;
EDIT: Your description isn't clear, but it's entirely possible that you really want:
编辑:您的描述不明确,但您完全有可能真正想要:
itemColour.Any(c => item.colour.Contains(c))
instead. Think logically about what you want to check, and apply it appropriately.
代替。从逻辑上思考您要检查的内容,并适当地应用它。
#2
2
Something like:
就像是:
from item in allItems
where itemColour.Any(x => x.Contains(item.colour))
where itemType.Any(x => x.StartsWith(item.type))
select new displayItem