Where in Clause in Linq

Where IN clause in LINQ

This expression should do what you want to achieve.

dataSource.StateList.Where(s => countryCodes.Contains(s.CountryCode))

LINQ to Entities - Where IN clause in query

Use Contains:

int[] ids = { 1, 2, 3, 4, 5};

var query = db.myTable.Where(item => ids.Contains(item.ID));

or in query syntax:

int[] ids = { 1, 2, 3, 4, 5};

var query = from item in db.myTable
where ids.Contains(item.ID)
select item;

Linq to Entities - SQL IN clause

You need to turn it on its head in terms of the way you're thinking about it. Instead of doing "in" to find the current item's user rights in a predefined set of applicable user rights, you're asking a predefined set of user rights if it contains the current item's applicable value. This is exactly the same way you would find an item in a regular list in .NET.

There are two ways of doing this using LINQ, one uses query syntax and the other uses method syntax. Essentially, they are the same and could be used interchangeably depending on your preference:

Query Syntax:

var selected = from u in users
where new[] { "Admin", "User", "Limited" }.Contains(u.User_Rights)
select u

foreach(user u in selected)
{
//Do your stuff on each selected user;
}

Method Syntax:

var selected = users.Where(u => new[] { "Admin", "User", "Limited" }.Contains(u.User_Rights));

foreach(user u in selected)
{
//Do stuff on each selected user;
}

My personal preference in this instance might be method syntax because instead of assigning the variable, I could do the foreach over an anonymous call like this:

foreach(User u in users.Where(u => new [] { "Admin", "User", "Limited" }.Contains(u.User_Rights)))
{
//Do stuff on each selected user;
}

Syntactically this looks more complex, and you have to understand the concept of lambda expressions or delegates to really figure out what's going on, but as you can see, this condenses the code a fair amount.

It all comes down to your coding style and preference - all three of my examples do the same thing slightly differently.

An alternative way doesn't even use LINQ, you can use the same method syntax replacing "where" with "FindAll" and get the same result, which will also work in .NET 2.0:

foreach(User u in users.FindAll(u => new [] { "Admin", "User", "Limited" }.Contains(u.User_Rights)))
{
//Do stuff on each selected user;
}

Where clause in Linq in List c#

Just put the Where before the Select:

var list=testList.Where(f=>f.Family=="").Select(n=>n.Name);

In Linq you need to apply the filter before projecting (unless the filter applies to the results of the projection rather than the original collection).

Linq Query with a Where clause in an Include statement

You cant have a Where inside the Where, but you can use Any which will return a boolean

var result = ctx.Offenders
.Include(o => o.Fees)
.Include(o => o.ViolationOffenders)
.Include(o => o.ViolationOffenders.Select(of => of.Violation))
.Where(o => o.YouthNumber != "" && o.FirstName != ""
&& o.Fees.Any(f=> f.Amount != null)) // here
.ToList();

Proper LINQ where clauses

The second one would be more efficient as it just has one predicate to evaluate against each item in the collection where as in the first one, it's applying the first predicate to all items first and the result (which is narrowed down at this point) is used for the second predicate and so on. The results get narrowed down every pass but still it involves multiple passes.

Also the chaining (first method) will work only if you are ANDing your predicates. Something like this x.Age == 10 || x.Fat == true will not work with your first method.

Get value from Database to Linq where clause

Try to use Dynamic LINQ. This is a sample of code using Dynamic LINQ library:

var query = northwind.Products
.Where("CategoryID = 3 AND UnitPrice > 3")
.OrderBy("SupplierID");

In your case:

var whereCondition = db.FILTRE_LINKLER.Select(x => x.CONDITIONS).FirstOrDefault();
var product = (from d in db.PRODUCTS
select new ProductModel
{
Description= d.DESCRIPTION,
Brand= d.BRANDS.BRAND,
SefUrl = sef_url,
Name= d.NAME,

})
.Where(whereCondition);

UPDATE:

  1. At first you should download a code to use Dynamic queries by this link.

  2. Then use DynamicQueryable class from the project Dynamic Query

  3. Then use your dynamic queries with IQueryable<T>.

Let me show an example:

var persons = new List<Person>()
{
new Person(){Id = 1, FirstName = "1"},
new Person(){Id = 2, FirstName = "2"},
new Person(){Id = 3, FirstName = "3"}
};
var personWithIdTwo = persons
.AsQueryable()
.Where("Id==2");

UPDATE 1:

If you do not want to add class, then you can use an Expression Tree.

An example of an expression tree:

var propName = "STOCK"; // here you assign any value
var constValue = "50"; // here you assign any value
var param = Expression.Parameter(typeof(ProductModel), "p");
var exp = Expression.Lambda<Func<ProductModel, bool>>(
Expression.GreaterThan(
Expression.Property(param, propName),
Expression.Constant(constValue)
),
param
);
var product = (from d in db.PRODUCTS
where query
select new ProductModel
{
Description= d.DESCRIPTION,
Brand= d.BRANDS.BRAND,
SefUrl = sef_url,
Name= d.NAME,

}).Where(exp);

UPDATE 2:

You can download library Dynamic query through NuGet. So you should not create classes in your project:

Sample Image



Related Topics



Leave a reply



Submit