简体   繁体   English

c#lambda表达式如果条件在where里面

[英]c# lambda expression if condition inside a where

I have a scenario where I am using lambda expression to filter data by name, address and phone number. 我有一个场景,我使用lambda表达式按名称,地址和电话号码过滤数据。 But when one property has no value then my filter considers where property is null and it give wrong results. 但是当一个属性没有值时,我的过滤器会考虑属性为null的位置,并给出错误的结果。 Is there any option to remove a where condition out of three cases with a if condition if property is null. 如果属性为null,是否有任何选项可以使用if条件从三种情况中删除where条件。 I mean is there any case to add where/check only for object where properties having values? 我的意思是有任何情况下添加where / check仅用于具有值的属性的对象吗?

var filterCriteria ={
name: "abc",
address:"",
phone:""
}

var Details = _context.LogEntities
                    .Where(p => p.name == (filterCriteria.name))
                    .Where(p => p.address== (filterCriteria.address))
                    .Where(p => p.phone== (filterCriteria.phone))

                return Json(Details);

If you still want to return records when the name, address, or phone are null then just check for null in the condition. 如果您仍希望在名称,地址或电话为空时返回记录,则只需在条件中检查是否为空。

var Details = _context.LogEntities
                    .Where(p => p.name == null || p.name == (filterCriteria.name))
                    .Where(p => p.address == null || p.address == (filterCriteria.address))
                    .Where(p => p.phone == null || p.phone == (filterCriteria.phone))

                return Json(Details);

You can write an extension method to conditionally apply the filter: 您可以编写一个扩展方法来有条件地应用过滤器:

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

And your query becomes: 您的查询变为:

using static System.String;

...

var employees = _context
   .LogEntities
   .WhereIf(!IsNullOrEmpty(filterCriteria.name), e => e.name == filterCriteria.name)
   .WhereIf(!IsNullOrEmpty(filterCriteria.address), e => e.address == filterCriteria.address)
   .WhereIf(!IsNullOrEmpty(filterCriteria.phone), e => e.phone == filterCriteria.phone)
   .ToList();

When doing filtering, if what you want is to consider null properties in the filter to match any values in the corresponding property in the collection, you should check the null against the filter property, not on the collection item's property. 在进行过滤时,如果您想要的是在过滤器中考虑null属性以匹配集合中相应属性中的any值,则应该针对过滤器属性检查null,而不是对集合项的属性进行检查。 So for a collection like 所以对于像这样的集合

var myCollection = [
{name: "abc",address:"qwerty",phone:"123"},
{name: "abc",address:"xyz",phone:"456"},
{name: "efg",address:"cde",phone:"654"}]

and filter like 和过滤器一样

var filterCriteria ={name: "abc",address:"",phone:""}

with desired result like: 期望的结果如:

[{name: "abc",address:"qwerty",phone:"123"},
{name: "abc",address:"xyz",phone:"456"}]

Your code should be 你的代码应该是

var Details = _context.LogEntities
                .Where(p => (filterCriteria.name) == null || p.name == (filterCriteria.name))
                .Where(p => (filterCriteria.address) == null || p.address== (filterCriteria.address))
                .Where(p => (filterCriteria.phone) == null || p.phone== (filterCriteria.phone))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM