473,799 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ 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 OnCriteriaChang ed
method at the bottom that's getting out of hand (and I haven't finished all
combinations!)

public class ProjectPickerPr esenter
{
private readonly List<WddProject _allProjects;
public List<stringPref ixList { get; private set; }
public string PrefixFilter { get; set; }
public string SequenceNumberF ilter { get; set; }
public string DescriptionFilt er { get; set; }
public List<WddProject DisplayProjects { get; private set; }

public ProjectPickerPr esenter() {
_allProjects = new
OpenProjects(Co nnection.DbConn ection(Connecti on.Env.Dev_Iden tityDriven));
DisplayProjects = _allProjects;
PrefixList = new List<string>((f rom project in _allProjects orderby
project.Prefix select project.Prefix) .Distinct());
}

public void OnCriteriaChang e() {
// no filters
if( (PrefixFilter== null) && (SequenceNumber Filter==null) &&
(DescriptionFil ter==null)) {
DisplayProjects = _allProjects;
}
// one filter only
else if ((PrefixFilter != null) && (SequenceNumber Filter == null) &&
(DescriptionFil ter == null)) {
var result = _allProjects.Wh ere(project =>
project.Prefix. Equals(PrefixFi lter));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
else if ((PrefixFilter == null) && (SequenceNumber Filter != null) &&
(DescriptionFil ter == null)) {
var result = _allProjects.Wh ere(project =>
project.Sequenc eNumber.StartsW ith(SequenceNum berFilter));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
else if ((PrefixFilter == null) && (SequenceNumber Filter == null) &&
(DescriptionFil ter != null)) {
var result = _allProjects.Wh ere(project =>
project.Descrip tion.Contains(D escriptionFilte r));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
// two filters together
else if ((PrefixFilter != null) && (SequenceNumber Filter == null) &&
(DescriptionFil ter != null)) {
var result = _allProjects.Wh ere(project =>
project.Prefix. Equals(PrefixFi lter) &&
project.Descrip tion.Contains(D escriptionFilte r));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
}
}

Oct 9 '08 #1
1 1221
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 OnCriteriaChang ed
method at the bottom that's getting out of hand (and I haven't finished all
combinations!)
public void OnCriteriaChang e() {
// no filters
if( (PrefixFilter== null) && (SequenceNumber Filter==null) &&
(DescriptionFil ter==null)) {
DisplayProjects = _allProjects;
}
// one filter only
else if ((PrefixFilter != null) && (SequenceNumber Filter == null) &&
(DescriptionFil ter == null)) {
var result = _allProjects.Wh ere(project =>
project.Prefix. Equals(PrefixFi lter));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
else if ((PrefixFilter == null) && (SequenceNumber Filter != null) &&
(DescriptionFil ter == null)) {
var result = _allProjects.Wh ere(project =>
project.Sequenc eNumber.StartsW ith(SequenceNum berFilter));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
else if ((PrefixFilter == null) && (SequenceNumber Filter == null) &&
(DescriptionFil ter != null)) {
var result = _allProjects.Wh ere(project =>
project.Descrip tion.Contains(D escriptionFilte r));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
// two filters together
else if ((PrefixFilter != null) && (SequenceNumber Filter == null) &&
(DescriptionFil ter != null)) {
var result = _allProjects.Wh ere(project =>
project.Prefix. Equals(PrefixFi lter) &&
project.Descrip tion.Contains(D escriptionFilte r));
if (result != null) DisplayProjects = new
List<WddProject >(result);
}
}
}
How about writing a comparer method? Something like:

private bool Compare(WddProj ect project) {
return
(PrefixFilter == null || project.Prefix == PrefixFilter) &&
(SequenceNumber Filter == null ||
project.Sequenc eNumber.StartsW ith(SequenceNum berFilter)) &&
(DescriptionFil ter == null ||
project.Descrip tion.Contains(D escriptionFilte r));
}

public void OnCriteriaChang e() {
if( (PrefixFilter== null) && (SequenceNumber Filter==null) &&
(DescriptionFil ter==null)) {
DisplayProjects = _allProjects;
} else {
var result = _allProjects.Wh ere(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
14679
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 as many times that there are
3
22007
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 TELL ME IF IT IS POSSIBLE TO USE IT IN WHERE CLAUSE AND SOME ALTERNATIVE. QUERY: SELECT
4
2941
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 appreciated. Thank you.
16
3074
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 certificate at the end of the year. I have the code working fine, except for the fact that when I want to restrict the entries to awards between certain dates, even though I can use the restriction in the query that shows the actual records, when the...
2
10758
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 it is used. SQLSTATE=42703 SELECT DISTINCT S3.OPR_APPLICATION_NR, S3.APPLICATION_ID, S3.APPRAISAL_TYPE_CD, S3.Appraisal_Used_Amount, S3.RPT_LEVEL2_NR,
9
19161
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 ------------------------- SELECT * FROM ( Select x.event_date From x FULL OUTER JOIN y ON x.event_date = y.event_date
8
3483
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 , etc. I want to go to the first row, do a WHERE statement, return the
5
2666
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, (@ageraw:=YEAR(CURRENT_DATE()) - YEAR(dateofbirth) -
0
1417
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 Server backend. There is a form in the .adp whose record source is a View which contains a WHERE clause. My questions (and sorry these may be kinda basic): - I am going to sort the view using a further query.. something like: "SELECT * from...
4
1743
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 Statements for primary key on Table "SCHEMA1 "."X2"
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
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 we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.