LINQ in Entity Framework附加到DATETIME WHERE子句

时间:2016-10-03 02:42:48

标签: c# entity-framework linq datetime lambda

我正在尝试在运行时构建一个WHERE子句。我用初始WHERE子句创建了一个查询:

var origSQL = (from ....
               join ...
               where ...
               select new { ... } ); 

并将以下WHERE添加到origSQL:

var endDate = (from table1 in db.Table1
               join aliasTable1 in db.Table1 on table1.id equals aliasTable1.id
               where aliasTable1.anotherId  == anInputStr
               && (table1.field1 == aliasTable1.field1)
               select DbFunctions.TruncateTime(aliasTable1.endDate)).Max();
...

在附加到原始WHERE之前,endDate需要进一步细化,例如:

if (!string.IsNullOrEmpty(anotherInputStr) && (anotherInputStr == "FOO"))
   endDate = endDate.Where ( a=> aliasTable1.GRADE == "F" );

在此,我收到错误:

DateTime does not contain a definition for Where accepting a first argument of type DateTime

所以我从初始的endDate中删除了选择部分,我想稍后可以添加它:

var endDateCriteria = from table1 in db.Table1
               join aliasTable1 in db.Table1 on table1.id equals aliasTable1.id
               where aliasTable1.anotherId  == anInputStr
               && (table1.field1 == aliasTable1.field1)

if (!string.IsNullOrEmpty(anotherInputStr) && (anotherInputStr == "FOO"))
   endDateCriteria = endDateCriteria .Where ( a=> aliasTable1.GRADE == "F" );

但是

.Where

仍然导致问题。我需要将一堆这些检查附加到此WHERE上,然后将其附加到原始查询中,并且无法找到正确的语法。

已添加这最初是一种自我加入。原始动态SQL看起来像这样

origSQL := ' AND table1.endDate = (
                 SELECT MAX(endDate)
                 FROM table1 aliasTable1   
                 WHERE aliasTable1.ID = table1.ID)

我试图将附加的WHERE子句附加到这个子句上,然后将其全部附加到主查询中。我不知道这个的语法。

2 个答案:

答案 0 :(得分:1)

每个查询都必须有一个select,它将定义变量将保存的内容。在你的第二个例子中你没有,所以没有编译。你应该这样做:

var endDateCriteria = from table1 in db.Table1
                        join aliasTable1 in db.Table1 on table1.id equals aliasTable1.id
                        where aliasTable1.anotherId == anInputStr
                        && (table1.field1 == aliasTable1.field1)
                        select new { table1, aliasTable1 };

if (!string.IsNullOrEmpty(anotherInputStr) && (anotherInputStr == "FOO"))
    endDateCriteria = endDateCriteria.Where(q => q.aliasTable1.GRADE == "F");

if (somethingElse)
    endDateCriteria = endDateCriteria.Where(q => q.aliasTable1.field1 > value);

if (otherCondition)
    endDateCriteria = endDateCriteria.Where(q => q.table1.field2 != value2);

DateTime result = endDateCriteria.Max(q => q.aliasTable1.endDate).Date;

这里我将endDateCriteria存储在一个匿名对象的两个表中,所以我可以稍后继续使用这两个表来表示我的条件,最后我得到了最大日期。我不认为需要使用TruncateTime

答案 1 :(得分:0)

原始海报的评论中提到的有条件的回答:

var endDates = (from table1 in db.Table1
               join table2 in db.Table2 on table1.id equals table2.id
               where table2.anotherId  == anInputStr
               && (table1.field1 == table2.field1);
if(YOUR CONDITION)
{
 endDates = endates.Where(e=> e.).Grade == "F")
}
var endate = from endates select DbFunctions.TruncateTime(table2.endDate)).Max();

您最初在单个对象上操作(Maximume table2.enddate).Where对集合起作用。

var endDate = (from table1 in db.Table1
               join table2 in db.Table2 on table1.id equals table2.id
               where table2.anotherId  == anInputStr
               && (table1.field1 == table2.field1)

&安培;&安培;桌子(我不知道哪张桌子).Grade ==" F"

               select DbFunctions.TruncateTime(table2.endDate)).Max();