使用Linq在C#中组合结果集

时间:2009-03-25 05:19:33

标签: c# .net linq

我有两个表'toc'和'content',结构如下:

TOC

ID
名(50)

含量

ID
文本(500)
标题(50)
tocid

我在toc.name,content.text和content.title中搜索一些文本,并且需要一个结果集。

是否可以使用linq(c#)组合搜索结果。我希望结果集如下:

id MatchedRecord tocid(来自toc的记录为null)
--- ------------------- ------
xx xxxxxxxxxxxxxxxxx xxxx

2 个答案:

答案 0 :(得分:1)

var tocs = from t in db.toc 
           where t.name.Contains("...")
           select new { id=toc.id
                       ,toc=t
                       ,content=(content) null
                       ,tocid=(int?) null
                      };

var contents = from c in db.content
               where c.text.Contains("...") || c.title.Contains("...")
               select new { id=c.id
                           ,toc=(toc) null
                           ,content=c
                           ,tocid=c.tocid
                          };

 var resultset = tocs.Union(contents);

答案 1 :(得分:0)

Bart De Smet在C#4.0中有一个新的linq operataor great blog post,它将完全按照您的意愿行事。他还展示了运营商添加到现有Linq的难易程度。

新方法将被称为Zip。不要与压缩混淆,它更像拉链的动作。