带 OR 的 linq where 语句

发布于 2024-10-20 02:03:20 字数 877 浏览 1 评论 0原文

我正在使用 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 中,我进行计数。例如,在一列中,我计算在给定日期设置的约会数量,在另一列中,我计算同一天参加的约会数量。日期 1 指安排约会的日期,日期 2 指出席约会的日期。例如,在 2011 年 3 月 3 日,我设置了 4 个约会(日期 1 是 2011 年 3 月 11 日),并且它们被设置为未来的各个日期(日期 2)。在同一天(3 月 3 日是 Date2),我还参加了过去在其他日期安排的几个其他约会。

感谢您的任何建议。

I'm using linq-to-sql for a query like this:

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{...};

How do I write this to make the OR and the AND statement work? I've tried putting a || and a && in place but it doesn't work and I'm stuck on this query.

Basically, it should return a list of days within a month and in the MyModel, I do counts. For instance, in a column I count the number of appointments set on a given day and in another column I count the number of appointments attended on the same day. Date1 refers to the date the appointments are set and Date2 refers to the dates the appointments are attended. So for example, on March 3rd 2011, I've set 4 appointments (Date1 is 3/11/2011 for these) and they're set for various dates in the future (Date2). During the same date (March 3rd is Date2 this time), I've also attended several other appointments that were set on other dates in the past.

Thanks for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2024-10-27 02:03:20
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 更改为 ||。

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{...};

Try removing the 4 extra "where" and change the OR to ||.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文