PC Review


Reply
Thread Tools Rate Thread

conditional where clause

 
 
Berryl Hesh
Guest
Posts: n/a
 
      10th Oct 2008
PROBLEM:
I want to create a linq query of an in memory object based on 3 possible
filters, and the only way I can think is something like the code below.
Besides being verbose, I'm repeating myself. How can I rewrite this??

Thanks for your help - BH

CODE:
I've included the whole class for context, but it's the OnCriteriaChanged
method at the bottom that's getting out of hand (and I haven't finished all
combinations!)

public class ProjectPickerPresenter
{
private readonly List<WddProject> _allProjects;
public List<string> PrefixList { get; private set; }
public string PrefixFilter { get; set; }
public string SequenceNumberFilter { get; set; }
public string DescriptionFilter { get; set; }
public List<WddProject> DisplayProjects { get; private set; }

public ProjectPickerPresenter() {
_allProjects = new
OpenProjects(Connection.DbConnection(Connection.Env.Dev_IdentityDriven));
DisplayProjects = _allProjects;
PrefixList = new List<string>((from project in _allProjects orderby
project.Prefix select project.Prefix).Distinct());
}

public void OnCriteriaChange() {
// no filters
if( (PrefixFilter==null) && (SequenceNumberFilter==null) &&
(DescriptionFilter==null)) {
DisplayProjects = _allProjects;
}
// one filter only
else if ((PrefixFilter != null) && (SequenceNumberFilter == null) &&
(DescriptionFilter == null)) {
var result = _allProjects.Where(project =>
project.Prefix.Equals(PrefixFilter));
if (result != null) DisplayProjects = new
List<WddProject>(result);
}
else if ((PrefixFilter == null) && (SequenceNumberFilter != null) &&
(DescriptionFilter == null)) {
var result = _allProjects.Where(project =>
project.SequenceNumber.StartsWith(SequenceNumberFilter));
if (result != null) DisplayProjects = new
List<WddProject>(result);
}
else if ((PrefixFilter == null) && (SequenceNumberFilter == null) &&
(DescriptionFilter != null)) {
var result = _allProjects.Where(project =>
project.Description.Contains(DescriptionFilter));
if (result != null) DisplayProjects = new
List<WddProject>(result);
}
// two filters together
else if ((PrefixFilter != null) && (SequenceNumberFilter == null) &&
(DescriptionFilter != null)) {
var result = _allProjects.Where(project =>
project.Prefix.Equals(PrefixFilter) &&
project.Description.Contains(DescriptionFilter));
if (result != null) DisplayProjects = new
List<WddProject>(result);
}
}
}


 
Reply With Quote
 
 
 
 
Göran Andersson
Guest
Posts: n/a
 
      10th Oct 2008
Reply in your other thread in the same group.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating conditional clause in a module Access Tinkerer Microsoft Access VBA Modules 2 13th Apr 2010 11:47 PM
Top Clause with GROUP BY clause? Dan Microsoft Access Queries 2 17th Dec 2008 07:27 PM
Can I use a between clause or in clause on an IF statement =?Utf-8?B?c3NjaWFycmlubw==?= Microsoft Excel Programming 2 4th May 2007 04:48 PM
Where Clause Tamer Microsoft Access Form Coding 4 6th Aug 2004 04:42 PM
Use of the LIKE clause in VBA Art Microsoft Access VBA Modules 4 3rd Apr 2004 12:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:31 AM.