0

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

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.