【问题标题】:Filtering by 1 boolean value from 2 values from interface从接口的 2 个值中过滤 1 个布尔值
【发布时间】:2011-06-16 16:47:25
【问题描述】:

假设我有一个IsRegistrationCompleted 的属性。

我的过滤器界面中有 2 个复选框,上面写着:

  1. 完成
  2. 不完整

如果两者都被选中,则同时检索完整用户和不完整用户。如果只选中Complete,则检索刚刚完成的用户。如果没有选中,则不会检索任何内容。

我如何把这个逻辑放在 linq 中?

我将我的复选框绑定如下:

public bool IsCompleted { get; set; }
public bool IsInCompleted { get; set; }

我的方法是:

private void GetUsers()
{
        //TODO: Apply filters here.
        var query = _context.GetUsers(); //Returns IQueryable<User>

        _context.Load(query, LoadBehavior.MergeIntoCurrent, LoadApplicantsCompleted, null);
}

将 silverlight 与 WCF Ria 服务结合使用。

【问题讨论】:

    标签: silverlight linq entity-framework wcf-ria-services


    【解决方案1】:

    这样的事情会奏效。如果你在一个 Where 语句中包含以下所有逻辑,我认为这将很难阅读

    if(IsCompleted && IsInCompleted)
    {
       query = query;
    }
    else if(IsCompleted)
    {
      query = query.Where(u => u.IsRegistrationCompleted);
    }
    else if(IsInCompleted)
    {
        query = query.Where(u => !u.IsRegistrationCompleted);
    }
    else
    {
      query = query.Where(u => false);
    }
    

    【讨论】:

      【解决方案2】:

      在一个查询中,它将如下所示:

      Users.Where(r => (IsCompleted  && r.IsRegistrationCompleted) || (IsInCompleted && !r.IsRegistrationCompleted))
      

      【讨论】:

        【解决方案3】:

        我还没有对此进行测试,但类似的方法可能会起作用:

        (from user in users where IsCompleted && user.IsCompleted select user).Union(from user in users where IsInCompleted && !user.IsCompleted select user).Distinct()
        

        【讨论】:

          猜你喜欢
          • 2018-05-20
          • 1970-01-01
          • 2017-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-02
          • 2021-10-21
          • 2020-05-15
          相关资源
          最近更新 更多