Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 作为func传递where子句_Entity Framework_Func - Fatal编程技术网

Entity framework 作为func传递where子句

Entity framework 作为func传递where子句,entity-framework,func,Entity Framework,Func,我将应用程序中的where子句分解到它们自己的库中,然后在运行时将它们传递到数据库。这样做是为了帮助测试 我在db上附加了一个日志以查看生成的sql是什么,我注意到where子句没有列出。数据仍然被过滤,这使我相信数据是在应用程序中过滤的,而不是在数据库中过滤的。有人能证实这一点吗?有更好的方法吗 以下是一个示例: Where子句 private Func<Message, bool> GetSearchWhere(string q, string type) { retur

我将应用程序中的where子句分解到它们自己的库中,然后在运行时将它们传递到数据库。这样做是为了帮助测试

我在db上附加了一个日志以查看生成的sql是什么,我注意到where子句没有列出。数据仍然被过滤,这使我相信数据是在应用程序中过滤的,而不是在数据库中过滤的。有人能证实这一点吗?有更好的方法吗

以下是一个示例:

Where子句

private Func<Message, bool> GetSearchWhere(string q, string type)
{
    return m => m.Name.Contains(q) && m.Type == type;
}
private Func GetSearchWhere(字符串q,字符串类型)
{
返回m=>m.Name.Contains(q)和&m.Type==Type;
}
DB呼叫

private List<Messages> GetMessages(Func<Message, bool> where)
{
     return Messaging.Messages.Where(where).ToList();
}
private List GetMessages(Func-where)
{
返回Messaging.Messages.Where(Where.ToList();
}

数据确实是通过LINQ to对象在内存中过滤的。当您将
Func
传递到
Where
方法时,实际上是在调用。如果要调用(从而在数据库中进行筛选),则需要传递
表达式m.Name.Contains(q)&&m.Type==Type;
}
私有列表GetMessages(表达式,其中)
{
返回Messaging.Messages.Where(Where.ToList();
}
private Expression<Func<Message, bool>> GetSearchWhere(string q, string type)
{
    return m => m.Name.Contains(q) && m.Type == type;
}

private List<Messages> GetMessages(Expression<Func<Message, bool>> where)
{
     return Messaging.Messages.Where(where).ToList();
}