C#Linq比较两个字符串数组并返回,如果任何字符串对两者都是通用的 [英] C# Linq to Compare two string arrays and return if any of the string is common to both

查看:270
本文介绍了C#Linq比较两个字符串数组并返回,如果任何字符串对两者都是通用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable,比如



dtInput

 --------- -------------------------------------------- 
CLASS_CODE COLUMN2 CPLUMN3

W,H,J,K,L xxxxxx xxxxxxx

R,G,S,F,R xxxxxx xxxxxxxx

W,Y ,M,C,E xxxxxx xxxxxxxx

---------------------------------- --------------------





我的字符串数组是



  string  [] Local =  new   string  [] {  W   M}; 



---------------------------------------------- ---------



var result =来自dtInput.AsEnumerable()中的temp

 其中​​ tem [   CLASS_CODE]。ToString()。分割(' ,')。包含(本地)
选择 tem;
dtInput = result.CopyToDataTable();





结果我期待的是



dtInput

 --------------------------- -------------------------- 
CLASS_CODE COLUMN2 CPLUMN3
W,H,J,K,L xxxxxx xxxxxxx
W,Y,M,C,E xxxxxx xxxxxxxx





因为这两行都包含以太W类或M类



请帮忙解决这个问题



添加代码块 - OriginalGriff [/ edit]

解决方案

只需使用 intersect

http://msdn.microsoft.com/en-us/library/system.linq.enumerable。相交%28v = vs.110%29.aspx [ ^ ]



示例:

http://www.dotnetperls.com/intersect [ ^ ]





祝你好运!

I have a DataTable like

dtInput

-----------------------------------------------------
CLASS_CODE    COLUMN2    CPLUMN3

W,H,J,K,L     xxxxxx     xxxxxxx

R,G,S,F,R     xxxxxx     xxxxxxxx

W,Y,M,C,E     xxxxxx     xxxxxxxx

------------------------------------------------------



and my string array is

string[] Local=new string[]{"W","M"};


-------------------------------------------------------

var result = from temp in dtInput.AsEnumerable()

             where tem["CLASS_CODE"].ToString().Split(',').Contains(Local)
             select tem;
dtInput=result.CopyToDataTable();



Result I am expecting is

dtInput

-----------------------------------------------------
CLASS_CODE    COLUMN2    CPLUMN3
W,H,J,K,L     xxxxxx     xxxxxxx
W,Y,M,C,E     xxxxxx     xxxxxxxx



Because the both row contain ether W class or M class

Please help to solve this

[edit]Code block added - OriginalGriff[/edit]

解决方案

Simply use intersect:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect%28v=vs.110%29.aspx[^]

Example:
http://www.dotnetperls.com/intersect[^]


Good luck!


这篇关于C#Linq比较两个字符串数组并返回,如果任何字符串对两者都是通用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆