【问题标题】:linq query with condition带有条件的 linq 查询
【发布时间】:2011-03-18 02:40:43
【问题描述】:

我正在用查询语法编写一个 linq 查询,我想知道如何添加另一个 where 子句。 基本上,我有以下几点:

var test = from t in MyDC.TheTable
           where t.UserID == TheUserID
           where t.DateDone.Date == TheDate.Date
           select new MyModel {.....};

TheTable 有一个名为 LinkedID 的列,该列也在另一个名为 ColorStatus 的表中(一个介于 1 和 10 之间的数字)。我希望编写 where 子句“ColorStatus 表中的 LinkedID 小于 7”。

谢谢。

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    只是关于改进您的陈述的建议。您实际上可以将两个 where 条件合并为一个条件。 && 表示“与”

    Where t.UserID == TheUserID && t.DateDone.Date = TheDate.Date
    

    【讨论】:

    • 好的,谢谢,我会改的。我仍在努力添加最后的 where 子句。
    【解决方案2】:

    您的信息“另一个名为 ColorStatus 的表”在这里没有意义。

    var test = from t in MyDC.TheTable
               where t.UserID == TheUserID
                  && t.DateDone.Date == TheDate.Date
                  && t.LinkedID < 7           
               select new MyModel {.....};
    

    可能我没有明白你的想法,这里有一个join 的例子可能对你有帮助。

    var test = from t in MyDC.TheTable
               join x in MyDC.ColorStatus
               on t.LinkedID == x.LinkedID
               where t.UserID == TheUserID
                  && t.DateDone.Date == TheDate.Date
                  && x.AnotherField == 1
               select new MyModel {.....};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多