gpt4 book ai didi

c# - 带 OR 的 linq where 语句

转载 作者:行者123 更新时间:2023-11-30 22:40:27 24 4
gpt4 key购买 nike

我正在使用 linq-to-sql 进行这样的查询:

public static List<MyModel> GetData(int TheUserID, DateTime TheDate)

using (MyDataContext TheDC = new MyDataContext())
{
var TheOutput = from a in TheDC.MyTable
where a.UserID == TheUserID
(where a.Date1.Month == TheDate.Month && where a.Date1.Year == TheDate.Year)
OR
(where a.Date2.Month == TheDate.Month && where a.Date2.Year == TheDate.Year)
group a by a.Date1.Date AND by a.Date2.Date into daygroups
select new MyModel{...};

我该如何编写才能使 OR 和 AND 语句起作用?我试过放一个||和一个 && 到位,但它不起作用,我被困在这个查询上。

基本上,它应该返回一个月内的天数列表,而在 MyModel 中,我会进行计数。例如,在一列中,我计算了在给定日期设置的约会数量,在另一列中,我计算了同一天参加的约会数量。 Date1 是指设置约会的日期,Date2 是指约会的参加日期。因此,例如,在 2011 年 3 月 3 日,我设置了 4 个约会(Date1 是 2011 年 3 月 11 日)并且它们被设置为 future 的不同日期(Date2)。在同一天(这次是 3 月 3 日是 Date2),我还参加了其他几个在过去其他日期安排的约会。

感谢您的任何建议。

最佳答案

using (MyDataContext TheDC = new MylDataContext())
{
var TheOutput = from a in TheDC.MyTable
where a.UserID == TheUserID &&
(a.Date1.Month == TheDate.Month && a.Date1.Year == TheDate.Year)
||
( a.Date2.Month == TheDate.Month && a.Date2.Year == TheDate.Year)
group a by a.Date1.Date AND by a.Date2.Date into daygroups
select new MyModel{...};

尝试删除 4 个额外的“where”并将 OR 更改为 ||。

关于c# - 带 OR 的 linq where 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5185402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com