无法过滤动态查询

时间:2015-10-01 09:42:34

标签: asp.net entity-framework dynamic

嗨,我对实体查询有疑问。请参阅我的代码

 var list = from table in db.USER_BETTINGS
                       where table.UserID == UserId
                       && table.UserID !="admin"
                           //&& table.WinningAfterRebate != 0m
                       && table.BettingTransactionTime >= fromDate &&
                         table.BettingTransactionTime <= toDAte
                       //&& table.WinningAfterRebate !=0m

                  //     orderby table.BettingTransactionNumber descending//table.BettingNumber descending//, table.BettingTransactionTime descending//
                       select table;

            if (loteryNumber != 0)
            {

              list=  list.Where(x => x.LotteryNumber == loteryNumber);
            }

            if (gameNum != 0)
            {

                list= list.Where(x => x.GameNumber == gameNum);
            }

            if (periodDate != "")
            {

               list= list.Where(x => x.PeriodDate == periodDate);
            }



            if (close.Equals("Y"))
            {

                list=  list.Where(w => w.WinningAfterRebate != 0m);
            }
            else
            {

                list= list.Where(x => x.WinningAfterRebate == 0);
            }

但是列表没有过滤,它返回所有记录?以前有人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:1)

您需要在此代码中进行一些改造。你已经不必要地使用了这么多if条件,我们需要首先摆脱它。使用以下内容更改您的查询。

var list =( from table in db.USER_BETTINGS
            where table.UserID == UserId
            && table.UserID !="admin"
            && table.BettingTransactionTime >= fromDate 
            && table.BettingTransactionTime <= toDAte
            && table.LotteryNumber == (loteryNumber != 0 ? loteryNumber : table.LotteryNumber)
            && table.GameNumber == (gameNum != 0 ? gameNum : table.GameNumber)
            && table.PeriodDate == (periodDate != string.Empty ? periodDate : table.PeriodDate )
            && (close.Equals("Y") ? table.WinningAfterRebate != 0 : table.WinningAfterRebate == 0)
            ).ToList();