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();
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.
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.
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.
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
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.
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();
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With