C# C语言中的动态where子句lambda或query#

C# C语言中的动态where子句lambda或query#,c#,entity-framework,dynamic,lambda,C#,Entity Framework,Dynamic,Lambda,我正在尝试编写动态lambda或查询,但出现了错误 对于lambda;我创建了一个函数 public IEnumerable<musteriler> GetCustomers<musteriler>(Expression<Func<musteriler, bool>> where) { IEnumerable<musteriler> _musteriler = market.musteriler.Where(wh

我正在尝试编写动态lambda或查询,但出现了错误

对于lambda;我创建了一个函数

    public IEnumerable<musteriler>  GetCustomers<musteriler>(Expression<Func<musteriler, bool>> where)
   {
    IEnumerable<musteriler> _musteriler = market.musteriler.Where(where).Select(m => m);

     return _musteriler;

    }
MAktif是数据库中musteriler表的列名。我尝试了其他专栏,但结果是一样的


我的错误在哪里

问题是
IQueryable。其中
是一个扩展方法,而
ObjectQuery。其中
在考虑扩展方法之前被选为“最佳重载方法匹配”

尝试:

public IEnumerable GetCustomers(表达式谓词)
{
return market.musteriler.AsQueryable().Where(谓词);
}

问题是
IQueryable。其中
是一个扩展方法,而
ObjectQuery。其中
在考虑扩展方法之前被选为“最佳重载方法匹配”

尝试:

public IEnumerable GetCustomers(表达式谓词)
{
return market.musteriler.AsQueryable().Where(谓词);
}
如果有效,你的方法应该是(如果仍然有意义的话)

public IEnumerable GetCustomers(string whereClause,params ObjectParameter[]参数){
return market.musteriler.Where(Where子句,参数).AsEnumerable();
}
如果有效,你的方法应该是(如果仍然有意义的话)

public IEnumerable GetCustomers(string whereClause,params ObjectParameter[]参数){
return market.musteriler.Where(Where子句,参数).AsEnumerable();
}

我想您需要在代码开头添加以下内容:

using System.Linq;

看起来您的市场是
ObjectContext
,musteriler是
ObjectSet
,它没有提供接受lambda的Where-您需要在
IQueryable
上扩展Where方法,该方法接受lambda表达式。

我想您需要在代码开头添加以下内容:

using System.Linq;

看起来您的市场是
ObjectContext
,musteriler是
ObjectSet
,它没有提供接受lambda的Where-您需要在
IQueryable
上扩展Where方法,该方法接受lambda表达式。

在我尝试的示例中,我得到了错误

方法“Where”不重载3个参数

使用SQLParametera使用我的where子句表达式

我发现我需要

使用System.Linq.Dynamic; 一旦我发现了,嗯


在我尝试的例子中,我得到了错误

方法“Where”不重载3个参数

使用SQLParametera使用我的where子句表达式

我发现我需要

使用System.Linq.Dynamic; 一旦我发现了,嗯



类似错误错误1“System.Data.Objects.ObjectQuery.Where(string,params System.Data.Objects.ObjectParameter[])”的最佳重载方法匹配有一些无效参数错误2参数1:无法从“System.Func”转换为“string”您是否有
使用System.Linq
像@Ladislav建议的那样?是的,我有。。。我所有的参考资料都在使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;再也没有了。。。错误2参数2:无法从“System.Func”转换为“System.Func”错误1“System.Collections.Generic.IEnumerable”不包含“Where”的定义以及“System.Linq.Enumerable”的最佳扩展方法重载。Where(System.Collections.Generic.IEnumerable,System.Func)具有一些无效参数,因此不能使用
AsEnumerable()
在这里,使用
AsQueryable()
。AsEnumerable将导致在应用“Where()”之前将整个表加载到内存中。类似错误1最佳重载方法匹配“System.Data.Objects.ObjectQuery.Where”(字符串,params System.Data.Objects.ObjectParameter[])'有一些无效参数错误2参数1:无法从'System.Func'转换为'string'您是否有
使用System.Linq
像@Ladislav建议的那样?是的,我有…我的所有引用都使用System;使用System.Collections.Generic;使用System.Linq;使用System.Text;再次没有…错误2参数2:无法从'System.Func'转换“System.Func”错误1“System.Collections.Generic.IEnumerable”不包含“Where”的定义和“System.Linq.Enumerable”的最佳扩展方法重载。Where(System.Collections.Generic.IEnumerable,System.Func)具有一些无效参数,因此不能使用
AsEnumerable()
此处,使用
AsQueryable()
.AsEnumerable将导致在应用“Where()”之前将整个表加载到内存中.market是我连接数据模型的数据库。我将其定义为private AkilliMarketEntities market;market是我连接数据模型的数据库。我将其定义为private AkilliMarketEntities market;感谢您的回答,但我得到了相同的错误。因为问题与返回类型无关。它表明问题在谓词中。一d与我在第一条消息中发布的相同错误using System;using System.Collections.Generic;using System.Linq;using System.Text;谢谢你Raphael,你的第二个代码也能工作:)非常感谢你的关注..谢谢你的回答,但我得到了相同的错误。因为问题不在于返回类型。。它表明问题在谓词中。。和我在第一个messageusing系统中发布的错误相同;使用System.Collections.Generic;使用System.Linq;使用系统文本;谢谢你,拉斐尔,你的第二个代码也很有用:)非常感谢你的有趣..是的,市场是一个objectcontext。。你能给我一个样品吗?yest market是一个objectcontext。。你能给我一个样品吗?这与这里的问题无关。这与这里的问题无关。
   Argument 1: cannot convert from 'System.Linq.Expressions.Expression<System.Func<musteriler,bool>>' to 'string'
 IEnumerable<musteriler> _musteriler=  market.musteriler.Where("MAktif = true").Select(m => m) as IEnumerable<musteriler>;
  'MAktif' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 6, column 1.
public IEnumerable<AkilliMarket.musteriler> GetCustomers<AkilliMarket.musteriler>(Expression<Func<AkilliMarket.musteriler, bool>> predicate)
{
   return market.musteriler.AsQueryable().Where(predicate);
}
public IEnumerable<musteriler>  GetCustomers<musteriler>(Expression<Func<musteriler, bool>> predicate)
       {
        return market.musteriler.AsQueryable().Where(predicate).AsEnumerable();

        }
 var _musteriler=  market.musteriler.Where("it.MAktif = @val", new ObjectParameter("val", true)).AsEnumerable().Count();
public IEnumerable<musteriler> GetCustomers(string whereClause, params ObjectParameter[] parameters) {
   return market.musteriler.Where(whereClause, parameters).AsEnumerable(); 
}
using System.Linq;