Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want select data witch for example sensor="sen" or Area="aa"
i write this code:
C#
var listsegment2 = from ls1 in SMDB.SegmentInfos
where (ls1.Area == area || string.IsNullOrEmpty(area)) && (ls1.Sensor == sen||string.IsNullOrEmpty(sen)) && (ls1.System == sys || string.IsNullOrEmpty(sys)) && (ls1.Jbo == jbo || string.IsNullOrEmpty(jbo))
select new
{}

but this code return andconditions result
help me to correct code,please
Posted
Updated 23-Sep-14 8:05am
v3

If I understood the question correctly, you have used && where you should've used ||

So something like this:
C#
var listsegment2 = from ls1 in SMDB.SegmentInfos
                   where (  (ls1.Area == area || string.IsNullOrEmpty(area)) 
                         || (ls1.Sensor == sen || string.IsNullOrEmpty(sen))
                   )
                   && (ls1.System == sys || string.IsNullOrEmpty(sys)) 
                   && (ls1.Jbo == jbo || string.IsNullOrEmpty(jbo))
select new
{}
 
Share this answer
 
Comments
Kim Togo 23-Sep-14 14:04pm    
From OP: when i used || return all record and incorrect result
Wendelius 23-Sep-14 14:35pm    
Thanks
Wendelius 23-Sep-14 14:36pm    
Without seeing your data and what are your expected results I'd say it's quite hard to show what would be the correct query. Perhaps if you post some sample data and what you're after :)
Member 11046620 24-Sep-14 8:47am    
iwant all condition in where
for examole all record where Area="AA" || Sensor="sen" || System="sys"||...
Wendelius 24-Sep-14 11:54am    
In that case what's wrong with a query like:

var listsegment2 = from ls1 in SMDB.SegmentInfos
where ls1.Area == "AA"
|| ls1.Sensor == "sen"
|| ls1.System == "sys"
select ls1;
If Sensor and Area can be "sen" or "aa", then just use OR = "||"

string.Equals is a great method to compare strings.

C#
string sensor = "sen";
string area = "aa";
string system = "sys";

var listSegment2 = from ls1 in SMDB.SegmentInfos
                   where string.Equals(ls1.Area, area, StringComparison.InvariantCultureIgnoreCase)
                   || string.Equals(ls1.Sensor, sen, StringComparison.InvariantCultureIgnoreCase)
                   || string.Equals(ls1.System, system, StringComparison.InvariantCultureIgnoreCase)
                   select ls1;

foreach (var segment in listSegment2)
{
  Console.WriteLine("Segment - Area:" + segment.Area + ", Sensor:" + segment.Sensor);
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900