日期:2014-05-20  浏览次数:20770 次

请加菲猫和吴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;