首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

请加菲猫和吴mm再来看下,还是 LINQ 的有关问题

2012-02-19 
请加菲猫和吴mm再来看下,还是 LINQ 的问题假设有两个数组:string[] keys new string[] { about, book

请加菲猫和吴mm再来看下,还是 LINQ 的问题
假设有两个数组:
string[] keys = new string[] { "about, "book", "carry", "crazy", "good", "old are", "windows" };
string[] inputarray = new string[] { "how old are you", "it is about 3", "I have a book", "carry", "it's time out", "a big box", "books", "linux" };
我希望通过 LINQ 查询,得到 inputarray 里面包括 keys 的项。

上面的应该返回
{ "how old are you", "it is about 3", "I have a book", "carry" }

请问有什么好办法?

[解决办法]

C# code
void Main(){    string[] keys = new string[] { "about", "book", "carry", "crazy", "good", "old are", "windows" };string[] inputarray = new string[] { "how old are you", "it is about 3", "I have a book", "carry", "it's time out", "a big box", "books", "linux" };var query=from input in inputarray          from ip in input.Split()          let key=(from k in keys from k0 in k.Split() select k0)          where key.Contains(ip)          select input;          foreach(var q in query.Distinct())          Console.WriteLine( q);          /*         how old are you        it is about 3        I have a book        carry          */}
[解决办法]
var result=from p in inputarray
from q in input
let key=(from k in q.Split(' ') select k)
where p.Contains(key)
select p;

热点排行