Using Linq for Multiple Condition in Where

using Linq with multiple where conditions

I will be following John Skeet answer as posted by Vossad01.

Proper Linq where clauses

Multiple condition on same column inside Linq where

AS already said, && operator means, that BOTH conditions has to be met. So in your condition it means that you want worktype type to by freelanceand fulltime at the same time, which is not possible :)

Most probably you want employees that have work type freelance OR fulltime, thus your condition should be:

people.Where(w=>w.worktype=="freelance" || w.worktype =="fulltime")

Or, if person can be set more than once in this table, then you could do:

people
.Where(w=>w.worktype=="freelance" || w.worktype =="fulltime")
// here I assume that you have name of a person,
// Basically, here I group by person
.GroupBy(p => p.Name)
// Here we check if any person has two entries,
// but you have to be careful here, as if person has two entries
// with worktype freelance or two entries with fulltime, it
// will pass condition as well.
.Where(grp => grp.Count() == 2)
.Select(grp => grp.FirstOrDefault());

LINQ query with multiple conditions in WHERE clause

Your sql is performing as you have written it. You need to restructure your query a little bit to express what you actually intended.

I tend to use a subquery approach like so:

IQueryable<long> clientsWithoutFeature = from cf in db.Features
where cf.Feature == 8 && !db.Features.Any(x => x.id == cf.id && x.Feature == 9)
select cf.Client;

IQueryable<long> clientsWithFeature = from cf in db.Features
where cf.Feature == 8 && db.Features.Any(x => x.id == cf.id && x.Feature == 9)
select cf.Client;

Im not really sure what your primary key column is. so i just guessed it was idY

Multiple condition in LINQ where clause

I hope you are doing this in a wrong way, you have to modify the where clause like this:

where c.AmoCode == 5 && 
(c.DateTime >= date1 && c.DateTime <= date2)

If you want to exclude the upper bounds and lower bounds of dates means remove the = sign from the comparison.

Linq multiple where clause with if condition

First, let's get the code formatted in a way that's not stupidly difficult to read:

List<MeetingVM> students = (
from s in db.Meetings
where MeetingIsActive == null || s.IsActive == MeetingIsActive
where MeetingStat == null || MeetingStat == 5 ? (DateTime.UtcNow >= s.MeetingStartTime && DateTime.UtcNow <= s.MeetingStopTime) : s.Status== MeetingStat
where StartDate == null || (s.MeetingStartTime >= StartDate && s.MeetingStartTime <= EndDate)
where s.Status!=4
orderby s.MeetingStartTime ascending
select new MeetingVM
{
MeetingStartTime = s.MeetingStartTime,
MeetingStopTime = s.MeetingStopTime,
Alias = s.Alias,
MeetingSubject = s.MeetingSubject,
UserId = s.UserId,
Status=s.Status

}).ToList();

That's a little better.

Now, I'm not 100% sure on this, so someone who knows better can feel free to correct me, but according to C# Operator Precedence, the OR ("||") operator has a higher precedence than the conditional ("?:") operator, so this line:

where MeetingStat == null || MeetingStat == 5 ? ...

is essentially getting evaluated like this:

where (MeetingStat == null || MeetingStat == 5) ? ...

If MeetingStat is equal to true, this would make the expression evaluate the first branch of the ternary operation. Judging from your description in your question, this is not what you want, so I recommend surrounding the ternary operation in parentheses to make your intention clear:

where MeetingStat == null || (MeetingStat == 5 ? ... )

How to Create a Linq Query with Multiple Conditional Where Clauses

Update this method like this:

public List<MYTABLE> Get(string filter1 = null,string filter2=null)
{
if(string.IsNullOrWhiteSpace(filter1) && string.IsNullOrWhiteSpace(filter2))
return new List<MYTABLE>();

IQueryable<MYTABLE> qry = db.MYTABLE.AsQueryable();
var searchPredicate = PredicateBuilder.False<MYTABLE>();

if (!string.IsNullOrWhiteSpace(filter1))
{
searchPredicate = searchPredicate.And(a => a.COLUMN1==(filter1);
}
if (!string.IsNullOrWhiteSpace(filter2))
{
searchPredicate = searchPredicate.And(a => a.COLUMN2==(filter2);
}
return qry.Where(searchPredicate).ToList();
}

Using linq for multiple condition in where

In your case, converting where statement as below is enough:

var filtred_list = oldList.Where(
c => conditions.Contains(c.attribut);
}
);

Using more than one condition in linq's where method

You can roll your separate conditions into a single predicate if you like:

codebase.Methods.Where(x => (x.Body.Scopes.Count > 5) && (x.Foo == "test"));

Or you can use a separate Where call for each condition:

codebase.Methods.Where(x => x.Body.Scopes.Count > 5)
.Where(x => x.Foo == "test");

LINQ query with a WHERE clause with multiple conditions

IMHO you should be OK with just this:

Database DB = new Database(); 
var result = DB.SomeClass.Where(x =>
Number == x.Class1.SomeNumber ||
Number == x.Class2.SomeNumber ||
Number == x.Class3.SomeNumber)
.ToList();

Your query loads all data and after that you evaluate condition in .NET = you must test null value prior to accessing SomeNumber but that is not needed if you evaluate SomeNumber in SQL through Linq-to-entities. Linq-to-entities should perform automatic null coalescing.

How to add multiple conditions in LINQ join using method syntax

It works now. It generates two queries, compared to many queries before, which is good.

Change:

r4 => new
{
keycolumn1 = r4.keycolumn
versioncolumn = r4.VersionNo
},
pppt => new
{
keycolumn1 = pppt.ppp.r2.KeyColumn
versioncolumn = r4.VersionNo
},

Final Code:

var result = 
repo1.Join(repo2, r1 => r1.KeyColumn, r2 => r2.KeyColumn, (r1, r2) => new { r1, r2 })
.Join(repo3, ppp => ppp.r2.KeyColumn, t => t.KeyColumn, (ppp, t) => new { ppp, t })
.Join(repo4,
pppt => new
{
keycolumn1 = pppt.ppp.r2.KeyColumn
versioncolumn = r4.VersionNo
},
r4 => new
{
keycolumn1 = r4.keycolumn
versioncolumn = r4.VersionNo
},
(pppt, r4) => new { pppt, r4 })
.Select(a => new MyObject
{
KeyColumn = a.ppp.r1.KeyColumn,
AnotherKeyColumn = a.ppp.r2.AnotherKeyColumn
})?.ToList();


Related Topics



Leave a reply



Submit