【问题标题】:LINQ query AND & ORLINQ 查询 AND & OR
【发布时间】:2014-10-20 11:11:49
【问题描述】:

我正在使用 .NET 开始我的旅程,我需要一些帮助。

我会举例说明我的情况,我有什么,我需要做什么,但我不知道该怎么做。

所以我有这样的课

public class Ban
{
    public int ID { get; set; }
    public string Nick { get; set; }
    public string IP { get; set; }
    public string GroupName { get; set; }
}

和变量 bansIQueryable

然后在签名方法中

public IEnumerable<Ban> FindBans(Ban filter);

我需要搜索那个 bans 变量;

我现在如何搜索

public IEnumerable<Ban> FindBans(Ban filter)
{
    var bans = GetBansQueryable();

    if (!string.IsNullOrWhiteSpace(filter.GroupName))
    {
        bans = bans.Where(b => b.GroupName == filter.GroupName);
    }
    if (!string.IsNullOrWhiteSpace(filter.Nick))
    {
        bans = bans.Where(b => b.Nick == filter.Nick);
    }
    if (!string.IsNullOrWhiteSpace(filter.IP))
    {
        bans = bans.Where(b => b.IP == filter.IP);
    }

    return bans.AsEnumerable();
}

使用 AND 进行过滤。 SQL查询部分会是这样的

... WHERE group_name = 'abc' AND nick = 'def' AND ip = 'ghi';

我需要的是

... WHERE group_name = 'abc' AND (nick = 'def' OR ip = 'ghi');

所有这些都需要是动态的(如果我们不通过 GroupName 就不要过滤它等) 除了手动制作这种动态之外,我不知道如何实现这一点

if (!string.IsNullOrWhiteSpace(filter.GroupName) && 
    string.IsNullOrWhiteSpace(filter.Nick) && 
    string.IsNullOrWhiteSpace(filter.IP))
{
    bans = bans.Where(b => b.GroupName == filter.GroupName);
}
else if (!string.IsNullOrWhiteSpace(filter.GroupName) && 
    !string.IsNullOrWhiteSpace(filter.Nick) && 
    string.IsNullOrWhiteSpace(filter.IP))
{
    bans = bans.Where(b => b.GroupName == filter.GroupName && b.Nick == filter.Nick);
}
else if (!string.IsNullOrWhiteSpace(filter.GroupName) && 
    !string.IsNullOrWhiteSpace(filter.Nick) && 
    !string.IsNullOrWhiteSpace(filter.IP))
{
    bans = bans.Where(b => b.GroupName == filter.GroupName && (b.Nick == filter.Nick || b.IP == filter.IP));
}

以此类推...现在将另一个变量添加到 Ban。

【问题讨论】:

  • 试试这个:.Where((b =&gt; b.GroupName =="abc") &amp;&amp; b.(nick =="def")||b.ip=="ghi")
  • @Thirisangu:这不处理空情况,就像在当前代码中一样。我认为SQL语句并不完全正确。
  • 我认为 PredicateBuilder 可以帮助你。 albahari.com/nutshell/predicatebuilder.aspx

标签: c# sql .net linq


【解决方案1】:

我认为您可以像这样简化整个约束:

bans = bans.Where(b => ( string.IsNullOrWhiteSpace(filter.GroupName) || b.GroupName == filter.GroupName )
                        &&
                        ( ( string.IsNullOrWhiteSpace(filter.Nick) || b.Nick == filter.Nick )
                          ||
                          ( string.IsNullOrWhiteSpace(filter.IP) || b.IP == filter.IP )
                        )
                 );

【讨论】:

  • 如果 GroupName 为空,Nick 为空且 IP 为“1.1.1.1”,则检查 IP 的部分甚至不会被评估。我认为,因为它不起作用。
  • @user2825007:你确定。 LINQ Where 在枚举项目时进行评估,对。
  • 是的。 i.imgur.com/qzKbkXn.png 第一个(在本例中为名称)为空,因此将其评估为 true,因此忽略 OR 语句的其余部分。
  • @user2825007:这就是or 背后的一般概念,对吧?
【解决方案2】:

您可能想查看Scott Hansleman 关于动态 sql、谓词构建器和 linqkit 的博文:

The Weekly Source Code 48 - DynamicQueryable makes custom LINQ expressions easier

否则有一篇非常不错的博客文章,介绍了将动态过滤器与 Kendo UI 网格和 Web Api 一起使用:

Kendo UI Open Sources Dynamic LINQ Helpers

【讨论】:

    【解决方案3】:

    您可以特例同时知道 nick 和 ip 的情况:

    public IEnumerable<Ban> FindBans(Ban filter)
    {
        var bans = GetBansQueryable();
    
        if (!string.IsNullOrWhiteSpace(filter.GroupName))
        {
            bans = bans.Where(b => b.GroupName == filter.GroupName);
        }
    
        if (!string.IsNullOrWhiteSpace(filter.Nick) && !string.IsNullOrWhiteSpace(filter.IP))
        {
            bans = bans.Where(b => b.Nick == filter.Nick || b.IP == filter.IP);
        }
        else if (!string.IsNullOrWhiteSpace(filter.Nick))
        {
            // filter.IP is empty
            bans = bans.Where(b => b.Nick == filter.Nick);
        }
        else if (!string.IsNullOrWhiteSpace(filter.IP))
        {
            // filter.Nick is empty
            bans = bans.Where(b => b.IP == filter.IP);
        }
    
        return bans.AsEnumerable();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多