日期的动态 Linq

发布于 2024-12-07 05:40:36 字数 566 浏览 0 评论 0原文

我想构建动态 Linq。以下是我的代码,在某个日期工作正常。但用户可以从日历中选择多个日期。我需要为所有这些选定的日期制作 Linq。

saleDate = calendarSales.SelectedDate;
List<SaleDetails> saleDetials = new List<SaleDetails>();
saleDetials = GetSaleDetails();
saleDetials.Where(sale => (Convert.ToDateTime(sale.DATE_TIME).Day == saleDate.Day &&
                           Convert.ToDateTime(sale.DATE_TIME).Month == saleDate.Month && 
                           Convert.ToDateTime(sale.DATE_TIME).Year == saleDate.Year)
                  ).ToList();

如何更新此查询?

I want to build dynamic Linq. Following is my code which works fine for one date. But user can select many dates from calendar. And I need to make Linq for all those selected dates.

saleDate = calendarSales.SelectedDate;
List<SaleDetails> saleDetials = new List<SaleDetails>();
saleDetials = GetSaleDetails();
saleDetials.Where(sale => (Convert.ToDateTime(sale.DATE_TIME).Day == saleDate.Day &&
                           Convert.ToDateTime(sale.DATE_TIME).Month == saleDate.Month && 
                           Convert.ToDateTime(sale.DATE_TIME).Year == saleDate.Year)
                  ).ToList();

How to update this query?

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

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

发布评论

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

评论(2

难以启齿的温柔 2024-12-14 05:40:36

您必须动态地为 where 子句构建谓词。

看一下谓词构建器

编辑

因为 PredicateBuilder 支持 ANDOR 运算符。

使用 OR 时,您必须从 False 的初始值开始:

// building the predicate
var pred = PredicateBuilder.False<SaleDetails>();
foreach (var date in MyDateList) 
{
    pred = pred.Or(sale => sale.DATE_TIME.Date == saleDate.Date);
}

// finally get the data and filter it by our dynamic predicate
List<SaleDetails> saleDetails = GetSaleDetails().Where(pred).ToList();

You have to build the predicate for your where clause dynamically.

Take a look at the predicatebuilder.

EDIT

Of cause PredicateBuilder supports AND and OR operators.

When using OR you have to start with the initial value of False:

// building the predicate
var pred = PredicateBuilder.False<SaleDetails>();
foreach (var date in MyDateList) 
{
    pred = pred.Or(sale => sale.DATE_TIME.Date == saleDate.Date);
}

// finally get the data and filter it by our dynamic predicate
List<SaleDetails> saleDetails = GetSaleDetails().Where(pred).ToList();
岁月无声 2024-12-14 05:40:36

我不确定您是否需要动态 LINQ。您应该能够检查销售的Where与所选日期的Any相匹配,如下所示:

var saleDates = GetSelectedDate();

List<SaleDetails> saleDetials = new List<SaleDetails>();
saleDetials = GetSaleDetails();
saleDetials.Where(sale => saleDates.Any(date => 
   (Convert.ToDateTime(sale.DATE_TIME).Day == date.Day &&
    Convert.ToDateTime(sale.DATE_TIME).Month == date.Month && 
    Convert.ToDateTime(sale.DATE_TIME).Year == date.Year)
    )).ToList();

或检查Date属性:

var saleDates = GetSelectedDate();

List<SaleDetails> saleDetials = new List<SaleDetails>();
saleDetials = GetSaleDetails();
saleDetials.Where(sale => saleDates.Any(date => 
   Convert.ToDateTime(sale.DATE_TIME).Date == date.Date)).ToList();

I'm not sure you need dynamic LINQ here. You should be able to check Where the sale matches Any of the selected dates, like so:

var saleDates = GetSelectedDate();

List<SaleDetails> saleDetials = new List<SaleDetails>();
saleDetials = GetSaleDetails();
saleDetials.Where(sale => saleDates.Any(date => 
   (Convert.ToDateTime(sale.DATE_TIME).Day == date.Day &&
    Convert.ToDateTime(sale.DATE_TIME).Month == date.Month && 
    Convert.ToDateTime(sale.DATE_TIME).Year == date.Year)
    )).ToList();

or checking on the Date property:

var saleDates = GetSelectedDate();

List<SaleDetails> saleDetials = new List<SaleDetails>();
saleDetials = GetSaleDetails();
saleDetials.Where(sale => saleDates.Any(date => 
   Convert.ToDateTime(sale.DATE_TIME).Date == date.Date)).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文