tags:

views:

266

answers:

2

I have two tables 'toc' and 'content' with the following structure:

toc

id
name(50)

content

id
text(500)
title(50)
tocid

I am searching the for some text within toc.name, content.text and content.title and require a single resultset.

Is it possible to combine the search results using linq(c#). I want the resultset as something like this:

id MatchedRecord tocid(null for records from toc)
--- ------------------- ------
xx xxxxxxxxxxxxxxxxx xxxx

+1  A: 
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);
Mark Cidade
A: 

Bart De Smet has a great blog post on a new linq operataor coming in C# 4.0 that will do exactly what you want to do. He also shows how easy the operator would be to add to existing Linq.

The new Method will be called Zip. Not to be confused with compression, its more like the action of a Zipper.

Tim Jarvis
Zip() won't return all the rows if one sequence is longer than the other.
Mark Cidade