Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statements within a Linq where clause

Struggling a bit today.

I have the following method that returns a list of products..lovely.

public static List<tblWeight> GetProductInfo(string memberid, string locationid, string basematerial, string source)
        {
             MyEntities getproductinfo = new MyEntities ();

            return (from p in getproductinfo .tblWeights
                        where    p.MemberId == memberid &&
                                 p.LocationId == locationid &&
                                 p.BaseMaterialName == basematerial &&
                                 p.WeightStatus == source
                       select p)
                       .ToList();
  • Where basematerial & source are drop down lists.

How do I go about incorporating a few IF statements into the where clause?

For example, if the basematerial ddl is not touched but an item in the source ddl is selected, the result would return everything associated with basematerial but filtered by the selected source.

Does that even make sense?!

I'm not even sure I am taking the correct approach - please forgive my ignorance.

like image 944
Ricardo Deano Avatar asked Jul 20 '10 14:07

Ricardo Deano


People also ask

What is the use of where clause in LINQ?

The criteria expressed as predicates, the where clause is not a mandatory clause of LINQ statements, but can be used to limit the number of records affected by a LINQ. The where clause is only used to extract records from select, delete, update and so on.

What is filter in LINQ where clause?

LINQ Where Clause Filtering Operator. A filtering operator in LINQ specifies the statement should only affect rows that meet specified criteria. The criteria expressed as predicates, the where clause is not a mandatory clause of LINQ statements, but can be used to limit the number of records affected by a LINQ.

What is LINQ where in SQL?

The LINQ Where is used to limit the number of records from select, update, delete statements. Given below are few examples of LINQ where code the list of elements in the collection. 1. Sequence of strings with single condition

What is the difference between LINQ where and method syntax?

It applies in both method and query syntax whereas method syntax requires the lambda expression and query syntax requires only the expression. The LINQ Where is used to limit the number of records from select, update, delete statements. Given below are few examples of LINQ where code the list of elements in the collection.


2 Answers

you can add them to your query on need:

var r =  (from p in getproductinfo .tblWeights 
                        where    p.MemberId == memberid && 
                                 p.LocationId == locationid && 
                                 p.WeightStatus == source 
                       select p) 

if (!String.IsNullOrEmpty(basematrial))
    r = r.Where(p => p.BaseMaterialName == basematerial);

return r.ToList();
like image 165
moi_meme Avatar answered Oct 18 '22 16:10

moi_meme


Consider implementing these extension methods named WhereIf.

You pass it two parameters: a statement evaluated to a boolean, and a lambda function. If the bool statement evaluates to true, the lambda is added.

WhereIf on ExtensionMethod.net

Your query could look like:

return getproductinfo.tblWeights
            .Where(w=> w.MemberId == memberid &&
                     w.LocationId == locationid)
            .WhereIf(!string.IsNullOrEmpty(basematerial), w=>w.BaseMaterialName == basematerial)
            .WhereIf(!string.IsNullOrEmpty(source), w=>w.WeightStatus == source)                         
            .ToList();

Here they are, for both IEnumerable and IQueryable. This allows you to use .WhereIf() in LINQ To SQL, Entity Framework, Lists, Arrays, and anything else that implements those 2 interfaces.

public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate)
{
    if (condition)
        return source.Where(predicate);
    else
        return source;
}

public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, int, bool> predicate)
{
    if (condition)
        return source.Where(predicate);
    else
        return source;
}

public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Func<TSource, bool> predicate)
{
    if (condition)
        return source.Where(predicate);
    else
        return source;
}

public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Func<TSource, int, bool> predicate)
{
    if (condition)
        return source.Where(predicate);
    else
        return source;
}
like image 20
p.campbell Avatar answered Oct 18 '22 16:10

p.campbell