我有以下代码:LINQ动态凡 - 不增加条款
public OTestTable GetTestCode(Func<TestTable, bool> whereClause)
{
return CoreContext.TestTables.Where(whereClause).Select(TestTableMap.DataToObject).FirstOrDefault();
}
CoreContext是
我TestTableMap如下我的数据上下文(在基类初始化):
public class TestTableMap
{
public static readonly Func<TestTable, OTestTable> DataToObject = mapper =>
new OTestTable
{
Code = mapper.mycode
};
}
然后在我的商业方法,我有以下几点:
public OTestTable GetTestCode(string code)
{
return QueryEngine.GetTestCode(id => id.mycode == code);
}
从我的主程序中,我使用字符串值调用GetTestCode。
当我看SQL事件探查器,我得到如下:
SELECT [t0].[mycode]
FROM [dbo].[TestTable] AS [t0]
它不必附加到SQL查询的WHERE子句。如果我将where子句添加到LINQ中作为var query = from DataContext.TestTable其中c.mycode ==''select c;
它会添加where子句。但是,当我运行我的代码时,它将返回正确的记录,但它好像是从数据库中撤回所有记录并在代码中进行筛选(不应该发生这种情况)。
任何想法与我做错了什么?
谢谢
那就是它!我完全忘记了使用Expression!谢谢一堆! – CodeLikeBeaker 2009-11-30 20:02:52