C# Linq where 子句.Contains(string[])

发布于 2024-10-26 04:42:37 字数 1341 浏览 1 评论 0原文

在过去的几个小时里,我一直在谷歌上搜索以找到问题的解决方案,但我被难住了。 我想知道是否有人可以帮助我解决以下问题。

我有一个针对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不甘平庸 2024-11-02 04:42:37

使其成为 LINQ to Objects 实际上意味着更容易回答。我认为你想要类似的东西:

from v in query.AsEnumerable()
let headers = v.Header36.Split(',')
where yy.BodyTypes.Intersect(headers).Any()
select new [...]

请注意,你的 (from yy in BodyTypes select yy) mostly 相当于 BodyTypes (至少它会以您当时使用它的方式给出) - 您不需要每次想做任何事情时都使用查询表达式。

这是一个稍微更有效的版本:

HashSet<String> bodyTypes = new HashSet<String>(yy.BodyTypes);
var query = from v in query.AsEnumerable()
            let headers =  v.Header36.Split(',')
            where bodyTypes.Overlaps(headers)
            select new [...]

Making it LINQ to Objects actually means it's easier to answer. I think you want something like:

from v in query.AsEnumerable()
let headers = v.Header36.Split(',')
where yy.BodyTypes.Intersect(headers).Any()
select new [...]

Note that your (from yy in BodyTypes select yy) is mostly equivalent to just BodyTypes (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:

HashSet<String> bodyTypes = new HashSet<String>(yy.BodyTypes);
var query = from v in query.AsEnumerable()
            let headers =  v.Header36.Split(',')
            where bodyTypes.Overlaps(headers)
            select new [...]
書生途 2024-11-02 04:42:37

尝试使用正则表达式和 .Any 方法:

where  (from yy in BodyTypes  
        select yy).Any( y => (new Regex(v.Header36.ToString())).IsMatch(y))

try using a regex and the .Any method:

where  (from yy in BodyTypes  
        select yy).Any( y => (new Regex(v.Header36.ToString())).IsMatch(y))
静水深流 2024-11-02 04:42:37

如果我理解了这一点,您的正文时间可能包含 1,2,3,4 并且您的 Header36 可能包含 2,5 并且您希望在某个项目出现在两个定界字符串?

改变你的where子句

where  (from yy in BodyTypes select yy)
    .Any(bts=> bts.Split(',').Any(bt=> v.Header36.Split(',').Contains(bt)) ) 

If i have understood this correct your body time might contain 1,2,3,4 and your Header36 might contain 2,5 and you want to match when an item appears in both delimted strings ?

Change your where clause

where  (from yy in BodyTypes select yy)
    .Any(bts=> bts.Split(',').Any(bt=> v.Header36.Split(',').Contains(bt)) ) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文