简体   繁体   English

如何使用LINQ查找与2个逗号分隔的字符串匹配

[英]How to find a match with 2 comma separated strings with LINQ

I am new to LINQ. 我是LINQ的新手。

I am trying to compare 2 comma separated strings to see if they contain a matching value. 我试图比较2个逗号分隔的字符串,看看它们是否包含匹配的值。

I have a string that contains a list of codes. 我有一个包含代码列表的字符串。 masterFormList = "AAA,BBB,CCC,FFF,GGG,HHH" masterFormList =“AAA,BBB,CCC,FFF,GGG,HHH”

I am trying to compare it to a list of objects. 我试图将它与对象列表进行比较。 In a given field FormCode contains a comma separated string of codes. 在给定字段中,FormCode包含逗号分隔的代码字符串。 I want to see if this at lease one code in this string is in the masterFormList. 我想看一下这个字符串中至少有一个代码是否在masterFormList中。 How would I write linq to accomplish this? 我怎么写linq来完成这个?

Right now I have: 现在我有:

resultsList = (from r in resultsList
where r.FormCodes.Split(',').Contains(masterFormList)
select r).ToList();

It does not return any matching items from the list. 它不会从列表中返回任何匹配的项目。

Please advise 请指教

You'd need to build a collection of the items to search for, then check to see if there are any contained within that set: 您需要构建要搜索的项目集合,然后检查该集合中是否包含任何项目:

var masterSet = new HashSet<string>(masterFormList.Split(','));

resultsList = resultsList
                 .Where(r => r.FormCodes.Split(',')
                              .Any(code => masterSet.Contains(code)))
                 .ToList();
var masterFormList = "AAA,BBB,CCC,FFF,GGG,HHH";
var otherList = "XXX,BBB,YYY";

bool match = otherList.Split(',').Intersect(masterFormList.Split(',')).Any();

or if you want the matching items 或者如果你想要匹配的项目

var matches = otherList.Split(',').Intersect(masterFormList.Split(',')).ToList();

To answer the question as stated, this will find all the matches between two strings: 要回答上述问题,可以找到两个字符串之间的所有匹配项:

var matches =
    from masterCode in masterFormList.Split(',')
    join formCode in formCodes.Split(',') on masterCode equals formCode 
    select formCode;
foreach (string match in matches)
{
    Console.WriteLine(match);
}

but that's overkill if all you want to know is that one exists. 但如果你想知道的是一个存在,这就太过分了。 You could just do this with the same query: 您可以使用相同的查询执行此操作:

Console.WriteLine(matches.Any());

However, that's likely to do more work than strictly necessary. 但是,这可能比完全必要的工作更多。 A modification to Reed Copsey's answer might be simplest (if we're looking to answer the question in the title of your post): 对Reed Copsey的答案的修改可能是最简单的(如果我们想要回答你帖子标题中的问题):

var masterSet = new HashSet<string>(masterFormList.Split(','));
bool atLeastOneMatch = formCodes.Split(',').Any(c => masterSet.Contains(c));

While these are reasonably idiomatic LINQ solutions to the problem you stated ("I am trying to compare 2 comma separated strings to see if they contain a matching value") they're probably not a great match for what you actually seem to want, which is to take a list of objects, and find just the ones in which a particular property meets your criteria. 虽然这些是你说过的问题的合理惯用LINQ解决方案(“我试图比较2个逗号分隔的字符串,看它们是否包含匹配值”),但它们可能不是你想要的东西的最佳匹配,是获取对象列表,并找到特定属性符合条件的对象。 And a join is probably the wrong approach for that because it looks rather unwieldy: 并且join可能是错误的方法,因为它看起来相当笨拙:

resultList =
    (from formItem in resultList
     from code in formItem.FormCodes.Split(',')
     join masterCode in masterFormList.Split(',') on code equals masterCode
     group code by formItem into matchGroup
     select matchGroup.Key)
    .ToList();

or if you prefer: 或者如果您愿意:

resultList =
    (from formItem in resultList
     from code in formItem.FormCodes.Split(',')
     join masterCode in masterFormList.Split(',') on code equals masterCode into matchGroup
     where matchGroup.Any()
     select formItem)
     .Distinct()
     .ToList();

These solutions have little to commend them... 这些解决方案几乎没有值得赞扬......

So given the problem evident from your code (as opposed to the problem defined in the question title and the first 3 paragraphs of your post), Reed Copsey's solution is better. 因此,从您的代码中可以看出问题(与问题标题中定义的问题以及帖子的前3段相反),Reed Copsey的解决方案更好。

The one tweak I'd make is that if your master set is fixed, you'd only want to build that HashSet<string> once, to amortize the costs. 我要做的一个调整是,如果您的主集已修复,您只需要构建一次HashSet<string> ,以分摊成本。 So either you'd put it in a static field: 所以要么你把它放在静态字段中:

private readonly static HashSet<string> masterSet =
    new HashSet<tring>(masterFormList.Split(',');

or use Lazy<T> to create it it on demand. 或使用Lazy<T>按需创建它。

(Edited 8/8/2013 after Reed pointed out to me in the comments that the problem evident from the code example was not the same as the problem stated in the question.) (2013年8月8日编辑后,Reed在评论中向我指出,代码示例中显而易见的问题与问题中陈述的问题不同。)

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

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