将比较函数和值作为参数传递到Entity Framework中 [英] Passing a comparison function and value as parameters into Entity Framework

查看:79
本文介绍了将比较函数和值作为参数传递到Entity Framework中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是的后续问题将比较函数作为参数传递到Entity Framework

我想将比较函数和值传递给查询实体框架的搜索函数。比较功能必须根据不同的属性操作。例如,我的数据是

I would like to pass a comparison function and a value into a search function that queries Entity Framework. The comparison function has to operate on different properties depending on the value. E.g., my data is

class DataItem
{
  [Key]
  public int ID { get; set; }
  public bool Active { get; set; }
  public string Prop1 { get; set; }
  public string Prop2 { get; set; }
}

我需要一个上下文方法

public List<DataItem> SearchValue(Func<string, string, bool> op, string value)
{
  if (value.Length < 10)
    return DataItem.Where(di => di.Active && op(di.Prop1, value)).ToList();
  else
    return DataItem.Where(di => di.Active && op(di.Prop2, value)).ToList();
}

我可以像

List<DataItem> list = context.SearchValue((s1, s2) => s1 == s2, "A");

我还需要不同的比较功能(所有规范)。

where I also need different comparison functions (all canonical).

程序编译好,但在运行时,我得到一个LINQ to Entities不支持LINQ表达式节点类型Invoke。错误。

Program compiles alright, but on running I get a "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities." error.

将DataItem加载到内存中不是一个选择,因为我将有大约十亿个。这就是为什么我正在寻找服务器端解决方案。我使用的所有操作都是规范的,所以可以将它们翻译成SQL查询。我的问题只是:我如何将它们作为参数传递?

Loading the DataItems into memory is not an option since I'll have around a billion of them. That's why I'm looking for a server-side solution. All the operations I'm using are canonical, so they can be translated into SQL queries. My question is just: how can I pass them as parameters?

推荐答案

不幸的是你不能用LinqToEntities来做。您必须手动创建sql查询或调用数据库函数...

Unfortunately you can't do it with LinqToEntities. You have to create sql query manually or call Database function...

这篇关于将比较函数和值作为参数传递到Entity Framework中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆