Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Complex Linq Query, building the Where Clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Harald SMS - 18 Oct 2007 10:03 GMT
When building a query for the LinkToSql a need to build a where clause that
has both individual values and range selections like this SQL Query:

- WHERE( (IdItem BETWEEN 1 AND 10 OR IdItem = 15 OR IdItem BETWEEN 30 AND
100 OR IdItem = 500... ) AND ( IdMenu BETWEEN 1 AND 5 OR) ...)

How can I write a LINQ statement that makes a efficient query like this?
Signature

Harald SMS

Harald SMS - 18 Oct 2007 10:13 GMT
The range and individual values are created at runtime and stored in memory,
so it is the dynamic buildiing of this staement that are a bit tricky.
Signature

Harald SMS

> When building a query for the LinkToSql a need to build a where clause that
> has both individual values and range selections like this SQL Query:
[quoted text clipped - 3 lines]
>
> How can I write a LINQ statement that makes a efficient query like this?
Jon Skeet [C# MVP] - 18 Oct 2007 15:34 GMT
On Oct 18, 10:13 am, Harald SMS <Harald...@discussions.microsoft.com>
wrote:
> The range and individual values are created at runtime and stored in memory,
> so it is the dynamic buildiing of this staement that are a bit tricky.

If you build it up using extension methods directly instead of a query
expression, it's not too bad:

var query = dataContext.WhicheverTable; // The source part

if (restrictByFirstCriterion)
{
   query = query.Where(item => item.Foo > wibble);
}
if (restrictBySecondCriterion)
{
   query = query.Where(item => item.Bar < 0);
}
etc

Basically keep applying Where clauses (or others, such as joins) as
appropriate.

Jon

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.