473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

where clause help

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<stringPrefixList { get; private set; }
public string PrefixFilter { get; set; }
public string SequenceNumberFilter { get; set; }
public string DescriptionFilter { get; set; }
public List<WddProjectDisplayProjects { get; private set; }

public ProjectPickerPresenter() {
_allProjects = new
OpenProjects(Connection.DbConnection(Connection.En v.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(SequenceNumberFi lter));
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);
}
}
}

Oct 9 '08 #1
1 1210
Berryl Hesh wrote:
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 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(SequenceNumberFi lter));
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);
}
}
}
How about writing a comparer method? Something like:

private bool Compare(WddProject project) {
return
(PrefixFilter == null || project.Prefix == PrefixFilter) &&
(SequenceNumberFilter == null ||
project.SequenceNumber.StartsWith(SequenceNumberFi lter)) &&
(DescriptionFilter == null ||
project.Description.Contains(DescriptionFilter));
}

public void OnCriteriaChange() {
if( (PrefixFilter==null) && (SequenceNumberFilter==null) &&
(DescriptionFilter==null)) {
DisplayProjects = _allProjects;
} else {
var result = _allProjects.Where(Compare);
if (result != null) DisplayProjects = result.ToList();
}
}

--
Göran Andersson
_____
http://www.guffa.com
Oct 10 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
4
by: shaun palmer | last post by:
when or Where do you use a union query ? how can I wright sql, tblPopulation,* tblcustomer,* one to one with all the appropriate attributes(field). Your help would be greatly...
16
by: Dixie | last post by:
I have a problem using Dev Ashish's excellent module to concatenate the results of a field from several records into one record. I am using the code to concatenate certain awards onto a...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
5
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser,...
0
by: webgirl | last post by:
I'm relatively new to SQL Server & looking for some guidance, if possible. I've been reading lots of different things & am a bit confused about some basics. I have an Access Project with SQL...
4
by: Bernard Dhooghe | last post by:
Table definition: CREATE TABLE "SCHEMA1 "."X2" ( "C1" CHAR(20) NOT NULL , "C2" CHAR(10) NOT NULL , "C3" CHAR(30) NOT NULL GENERATED ALWAYS AS (C1|| C2) ) IN "USERSPACE1" ; -- DDL...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.