C# linq 查询在比较日期时超时

问题描述 投票:0回答:1

我的一个应用程序中显示了此处的代码。 linq 查询无限期挂起。

public virtual List<TradeView> GetTradeViewByDatesDataSourceAndCommodityId(DateTime fromDate, DateTime toDate, int commodityId, int originId, int dataSourceId)
{
    var context = GetTradeEntities();

    var theList = (from tradeView in context.TradeViews
                   where (tradeView.MonthStartDate >= fromDate)
                         && (tradeView.MonthEndDate <= toDate)
                         && (tradeView.CommodityId == commodityId)
                         && (tradeView.OriginId == originId)
                         && (tradeView.DataSourceId == dataSourceId)
                   select tradeView).ToList();

    return theList;
}

我将其更改为此处所示 - 此后,

query1
工作正常(可能是由于所有整数比较),但
query2
失败。比较日期时似乎失败了。

public virtual List<TradeView> GetTradeViewByDatesDataSourceAndCommodityId(DateTime fromDate, DateTime toDate, int commodityId, int originId, int dataSourceId)
{
    var context = GetTradeEntities();

    var query1 = (from tradeView in context.TradeViews
                  where
                        && (tradeView.CommodityId == commodityId)
                        && (tradeView.OriginId == originId)
                        && (tradeView.DataSourceId == dataSourceId)
                  select tradeView);
    var theList = query1.ToList();

    var query2 = (from tradeView in theList
                  where (tradeView.MonthStartDate >= fromDate)
                         && (tradeView.MonthEndDate <= toDate)
                  select tradeView);
                           
    return query2.ToList();
}

我尝试将比较转换为仅日期 - 仍然没有运气。请帮忙。

c# entity-framework linq
1个回答
0
投票

我更改了代码,如下所示。然后就可以正常工作了。我将商品 ID 条件从查询 1 移至查询 2。我看不出有任何理由会因为商品状态而超时。它只是一个整数列。 谢谢大家的帮助。

        
        public virtual List<TradeView> GetTradeViewByDatesDataSourceAndCommodityId(DateTime fromDate, DateTime toDate, int commodityId, int originId, int dataSourceId)
{
    var context = GetTradeEntities();

    var query1 = (from tradeView in context.TradeViews
                  where
                         (tradeView.OriginId == originId)
                        && (tradeView.DataSourceId == dataSourceId)
                  select tradeView);
    var theList = query1.ToList();

    var query2 = (from tradeView in theList
                  where (tradeView.MonthStartDate >= fromDate)
                         && (tradeView.MonthEndDate <= toDate)
                         && (tradeView.CommodityId == commodityId)
                  select tradeView);
                           
    return query2.ToList();
} 

© www.soinside.com 2019 - 2024. All rights reserved.