C# Linq where 子句.Contains(string[])
在过去的几个小时里,我一直在谷歌上搜索以找到问题的解决方案,但我被难住了。 我想知道是否有人可以帮助我解决以下问题。
我有一个针对 DataTable 进行查询的 Linq 查询。我有一个 string[] BodyTypes 参数,其中包含“5,7,11”或“6,7,4”或“5”等字符串,
我拥有的 Linq 是:
var query2 = (from v in query.AsEnumerable()
where (from yy in BodyTypes
select yy).Contains(v.Header36.ToString())
select new
{
PartNumber = v.PartNumber,
Position = v.Position,
ImagePath = v.ImagePath,
Supplier = v.Supplier,
Price = v.Price,
RRP = v.RRP,
Stock = v.Stock,
BaseCat = v.BaseCat,
Description = v.Description,
Header36 = v.Header36,
GT_Id = v.GT_Id
});
v.Header36 为每个分配了不同的值行即“11,7,4,5”或“11,6,7”
我的问题是如何使用 Linq 进行匹配,我想将 Header36 与 string[]BodyTypes 数组中传递的任何内容相匹配,例如使用通配符或类似的声明。 我的问题是这个 DataTable 是从第 3 方的 Web 服务加载的,因此这里不能使用 SQL 后端。 任何建议将不胜感激。
亲切的问候 尼尔.
Ive been googling for the last few hours to find a solution to my problem but im stumpped.
I was wondering if someone could help me out with the following.
I have a Linq query that queries against a DataTable. I have a param of string[] BodyTypes that holds strings like "5,7,11" or "6,7,4" or just "5,"
the Linq i have is:
var query2 = (from v in query.AsEnumerable()
where (from yy in BodyTypes
select yy).Contains(v.Header36.ToString())
select new
{
PartNumber = v.PartNumber,
Position = v.Position,
ImagePath = v.ImagePath,
Supplier = v.Supplier,
Price = v.Price,
RRP = v.RRP,
Stock = v.Stock,
BaseCat = v.BaseCat,
Description = v.Description,
Header36 = v.Header36,
GT_Id = v.GT_Id
});
v.Header36 has different values assigned to it per row i.e. "11,7,4,5" or "11,6,7"
My problem is how do i make a match using Linq, i want to match Header36 with anything that is passed in the string[]BodyTypes array like using a wild card or like statement.
My problem is also this DataTable is loaded from a 3rd party's webservice, so no SQL backend can be used here.
Any suggestions would be greatfully appreciated.
Kind Regards
Neil.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使其成为 LINQ to Objects 实际上意味着更容易回答。我认为你想要类似的东西:
请注意,你的
(from yy in BodyTypes select yy)
mostly 相当于BodyTypes
(至少它会以您当时使用它的方式给出) - 您不需要每次想做任何事情时都使用查询表达式。这是一个稍微更有效的版本:
Making it LINQ to Objects actually means it's easier to answer. I think you want something like:
Note that your
(from yy in BodyTypes select yy)
is mostly equivalent to justBodyTypes
(at least it will be given the way you're then using it) - you don't need to use a query expression every time you want to do anything.Here's a slightly more efficient version:
尝试使用正则表达式和 .Any 方法:
try using a regex and the .Any method:
如果我理解了这一点,您的正文时间可能包含
1,2,3,4
并且您的 Header36 可能包含2,5
并且您希望在某个项目出现在两个定界字符串?改变你的where子句
If i have understood this correct your body time might contain
1,2,3,4
and your Header36 might contain2,5
and you want to match when an item appears in both delimted strings ?Change your where clause