【问题标题】:LinQ Query Where ConditionLinQ 查询 Where 条件
【发布时间】:2018-06-23 20:54:31
【问题描述】:

我正在尝试将数据导入新数据库,但旧数据库中的某些列为空。

在我的一种方法中,我使用下面的查询来获取记录,就像我说的那样,行中已经有一列对于某些记录具有空值。

Guid nameGuid= new guid('CCECE54B-EE14-4463-8A0B-02C72679334A')

MySubQuery = from a in MainQuery
              where a.Table1.Id.Equals(nameGuid)
        Select a;

我想检查 a.Table1.Id 值,如果它等于 null,那么我仍然想要该行但忽略 where 条件。

关于在 Linq 查询中使用三元运算符或任何其他方法来完成我的任务的任何建议。

【问题讨论】:

    标签: c# .net linq


    【解决方案1】:

    听起来像你想要的:

    MySubQuery = from a in MainQuery
                 where a.TableId.Id Is Nothing OrElse a.Table1.Id.Equals(nameGuid)
    

    假设我的 VB 是正确的......在 C# 中我只是写:

    var query = from a in mainQuery
                where a.TableId.Id == null || a.TableId.Id == nameGuid
                select a;
    

    或者直接使用扩展方法:

    var query = mainQuery.Where(a => a.TableId.Id == null || 
                                     a.TableId.Id == nameGuid);
    

    【讨论】:

      【解决方案2】:

      怎么样:

      MySubQuery = from a in MainQuery
                   where a.Table1.Id == null || a.Table1.Id.Equals(nameGuid)
                   select a;
      

      还是我错过了什么?

      编辑:

      我缺少的是斯基特先生发现的东西。是VB。好吧,我会把它作为 C# 示例保留下来。

      【讨论】:

        【解决方案3】:
         var datarows = (from row in dt.AsEnumerable()
                                    where row.Field<Guid>("Id") == new 
                         Guid(ddlCode.SelectedValue.ToString())
                                    select row);
        

        之后

             DataTable newDt=datarows.CopyToDataTable();
             var sampleName=newDt.Rows[0]["SampleName"].ToString(); 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-05-09
          • 1970-01-01
          • 2020-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-30
          相关资源
          最近更新 更多