linq函数处理:两个字段等于或都为null或空 [英] linq function to handle : both fields equal or both null or empty

查看:181
本文介绍了linq函数处理:两个字段等于或都为null或空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个LINQ查询,我比较的对象与存储在我的数据库中的所有其他实体。名和姓是必填字段,所以我不必检查空。但在街道的情况下,我必须做这些检查。



我想匹配,如果这两个字段为空或空字符串,或者说,他们是相同的。下面的LINQ查询工作正常。



但我徘徊是不是有什么办法,使其更具可读性。例如像自定义函数的布尔FieldsAreEqualOrBothNullOrEmpty(r.street,request.street)



想不通怎么做,如果这是可能的。从研发

  VAR相同= 
在db.Requests
,其中r.firstName.ToLower()= = request.firstName.ToLower()
和;&安培; r.lastName.ToLower()== request.lastName.ToLower()

//似乎长,只是比较两个字段
和;&安培; (string.IsNullOrEmpty(r.street)及和放大器; string.IsNullOrEmpty(request.street))
|| r.street.ToLower()== request.street.ToLower()

选择R;
返回相同的;


解决方案

我将简化:

$ B $从R b

  VAR相同= 
在db.Requests
,其中r.firstName.ToLower()== request.firstName.ToLower( )
和;&安培; r.lastName.ToLower()== request.lastName.ToLower()
选择R;

如果(string.IsNullOrEmpty(request.street)){
相同= same.Where(R = GT; string.IsNullOrEmpty(r.street));
}其他{
串街道= request.street.ToLower();
相同= same.Where(R = GT; r.street.ToLower()==街道);
}



关于这样做的好处是,它使查询在每种情况下简单。
如果您选择,您可以使用类似的逻辑前两个,而且它的可能的也被移动到一个基于表达式的实用方法。未经测试的:

 公共静态的IQueryable< T> FieldsAreEqualOrBothNullOrEmpty< T>(
本的IQueryable< T>源,
表达式来; Func键< T,串>>成员,字符串值)
{
表达的身体;
如果(string.IsNullOrEmpty(值))
{
=身体Expression.Call(
typeof运算(字符串),IsNullOrEmpty,空,member.Body);
}
,否则
{
=身体Expression.Equal(
Expression.Call(member.Body,ToLower将空),
表达。恒(value.ToLower(),typeof运算(字符串)));
}

返回source.Where(Expression.Lambda<&Func键LT; T,BOOL>>(
身上,member.Parameters));
}



然后:

  VAR相同= db.Requests 
.FieldsAreEqualOrBothNullOrEmpty(X => x.firstName,request.firstName)
.FieldsAreEqualOrBothNullOrEmpty(X => x.lastName, request.lastName)
.FieldsAreEqualOrBothNullOrEmpty(X => x.street,request.street);


Hi I have a linq query where I compare an object with all other entity stored in my database. Firstname and lastname are mandatory fields so I don't have to check for null. But in the case of street I have to do these checks.

I want to match if both fields are null or empty strings or that they are the same. The below linq query is working fine.

But I was wandering isn't there any way to make it more readable. For example with a custom function like Bool FieldsAreEqualOrBothNullOrEmpty(r.street, request.street)

Can't figure how to do that and if it's possible.

var same = 
   from r in db.Requests
   where r.firstName.ToLower() == request.firstName.ToLower()
      && r.lastName.ToLower() == request.lastName.ToLower()

      //Seems long to just compare two fields
      && ( string.IsNullOrEmpty(r.street) && string.IsNullOrEmpty(request.street) ) 
         || r.street.ToLower() == request.street.ToLower()

      select r;
return same;

解决方案

I would simplify:

var same = 
   from r in db.Requests
   where r.firstName.ToLower() == request.firstName.ToLower()
      && r.lastName.ToLower() == request.lastName.ToLower()
   select r;

if(string.IsNullOrEmpty(request.street)) {
    same = same.Where(r => string.IsNullOrEmpty(r.street));
} else {
    string street = request.street.ToLower();
    same = same.Where(r => r.street.ToLower() == street);
}

The nice thing about the this is that it keeps the query simple in each case. You could use similar logic for the first two if you choose, and it could also be moved to an expression-based utility method. Untested:

public static IQueryable<T> FieldsAreEqualOrBothNullOrEmpty<T>(
    this IQueryable<T> source,
    Expression<Func<T, string>> member, string value)
{
    Expression body;
    if (string.IsNullOrEmpty(value))
    {
        body = Expression.Call(
            typeof(string), "IsNullOrEmpty", null, member.Body);
    }
    else
    {
        body = Expression.Equal(
            Expression.Call(member.Body, "ToLower", null),
            Expression.Constant(value.ToLower(), typeof(string)));
    }

    return source.Where(Expression.Lambda<Func<T,bool>>(
        body, member.Parameters));
}

then:

var same = db.Requests
           .FieldsAreEqualOrBothNullOrEmpty(x => x.firstName, request.firstName)
           .FieldsAreEqualOrBothNullOrEmpty(x => x.lastName, request.lastName)
           .FieldsAreEqualOrBothNullOrEmpty(x => x.street, request.street);

这篇关于linq函数处理:两个字段等于或都为null或空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆