LINQ中的复合式子句无法正常工作

时间:2019-06-05 22:09:47

标签: linq entity-framework-6 linq-to-entities

我有一个屏幕,用户可以在其中输入不同的值以添加到WHERE子句中。当我仅使用1值进行测试时,我的代码可以正常工作。当我使用1个以上的值进行测试时,结果集始终为0。

我已经验证了数据库中的数据,并且来自表单的数据符合预期。它们在WHERE谓词中的多个项目不正确匹配。

whereClause的字典类型

      var lazydata = (from item in db.ComplaintLogWithRetailer
                            select item);
            string val = "";

            if (whereClause != null)
            {
                if (whereClause.Keys.Contains("CaseYear"))
                {
                    val = whereClause["CaseYear"];
                    lazydata = lazydata.Where(w => w.CaseYearOnly.ToString().Equals(val));
                }

                if (whereClause.Keys.Contains("AssignedTo"))
                {
                    val = whereClause["AssignedTo"];
                    lazydata = lazydata.Where(w => w.Initials.ToString().Equals(val));
                }

                if (whereClause.Keys.Contains("CaseNumber"))
                {
                    val = whereClause["CaseNumber"];

                    lazydata = lazydata.Where(w => w.CaseNumber.ToString().Equals(val));
                }


                //if (whereClause.Keys.Contains("CountyId"))
                //    data = lazydata.Where(w => w.Count.ToString().Equals(whereClause["CountyId"])).ToList();


                if (whereClause.Keys.Contains("ClassCode"))
                    lazydata = lazydata.Where(w => w.ClassCode.ToString().Equals(whereClause["ClassCode"]));


                if (whereClause.Keys.Contains("DateReceived"))
                    lazydata.Where(w => w.ReceivedDate.ToString().Equals(whereClause["DateReceived"]));


                if (whereClause.Keys.Contains("RetailerNumber"))
                    lazydata = lazydata.Where(w => w.RetailerNumber.ToString().Equals(whereClause["RetailerNumber"]));


                data = lazydata.ToList();

                //data = lazydata.ToList<ComplaintLogWithRetailer>();

                //data = (from item in lazydata
                //        select item).ToList();
            }

有5条记录的年份为2018,其中1条记录的AssignedTo为'BCC',我希望有1条记录返回,但是当执行2个或更多'.Where'操作时,总会得到0条记录

0 个答案:

没有答案