动态where子句lambda或查询在C# [英] Dynamic where clauses lambda or query in C#
问题描述
我正在尝试写一个动态的lambda或者查询,但是它发生错误..
I am trying to write a dynamic lambda or query but it occurs an error..
我创建了一个函数
public IEnumerable<musteriler> GetCustomers<musteriler>(Expression<Func<musteriler, bool>> where)
{
IEnumerable<musteriler> _musteriler = market.musteriler.Where(where).Select(m => m);
return _musteriler;
}
我打电话给这样
IEnumerable<musteriler> _musteriler = helper.GetCustomers<musteriler>(m => m.MAktif == true);
我在Where(其中)中有两个错误, >
I get two errors in Where(where) which are
The best overloaded method match for System.Data.Objects.ObjectQuery<AkilliMarket.musteriler>.Where(string, params System.Data.Objects.ObjectParameter[])' has some invalid arguments
和
Argument 1: cannot convert from 'System.Linq.Expressions.Expression<System.Func<musteriler,bool>>' to 'string'
在我尝试过一个字符串查询,如
after I tried a string query like
IEnumerable<musteriler> _musteriler= market.musteriler.Where("MAktif = true").Select(m => m) as IEnumerable<musteriler>;
是的,它工作,但我不能使用_musteriler ..例如当我写_musteriler.Count();我收到这个错误
yes it works but I cant use _musteriler.. for example when I write _musteriler.Count(); I get this error
'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.
MAktif是db中的列表名称。我尝试了另一列,但结果是一样的。
MAktif is a column name of my musteriler table in db. and I tried another columns but result is same..
我的错误在哪里?
推荐答案
问题是 IQueryable< T> .Where
方法和 ObjectQuery.Where
在考虑扩展方法之前被选为最佳重载方法匹配。
The problem is IQueryable<T>.Where
is an extension method and ObjectQuery.Where
is picked as "best overloaded method match" before extension methods are considered.
尝试:
public IEnumerable<AkilliMarket.musteriler> GetCustomers<AkilliMarket.musteriler>(Expression<Func<AkilliMarket.musteriler, bool>> predicate)
{
return market.musteriler.AsQueryable().Where(predicate);
}
这篇关于动态where子句lambda或查询在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!