Dynamic where clauses lambda or query in C#

雨燕双飞 提交于 2019-12-11 05:45:15

问题


I am trying to write a dynamic lambda or query but it occurs an error..

for lambda; I created a function

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

     return _musteriler;

    }

and I call like that

  IEnumerable<musteriler> _musteriler  = helper.GetCustomers<musteriler>(m => m.MAktif == true);

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

and

   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>;

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 is a column name of my musteriler table in db. and I tried another columns but result is same..

Where are my mistakes for both?


回答1:


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.

Try:

public IEnumerable<AkilliMarket.musteriler> GetCustomers<AkilliMarket.musteriler>(Expression<Func<AkilliMarket.musteriler, bool>> predicate)
{
   return market.musteriler.AsQueryable().Where(predicate);
}



回答2:


public IEnumerable<musteriler>  GetCustomers<musteriler>(Expression<Func<musteriler, bool>> predicate)
       {
        return market.musteriler.AsQueryable().Where(predicate).AsEnumerable();

        }

if it doesn't work

can you just try

 var _musteriler=  market.musteriler.Where("it.MAktif = @val", new ObjectParameter("val", true)).AsEnumerable().Count();

and if it works, your method should be (if there's still a sense to make a method)

public IEnumerable<musteriler> GetCustomers(string whereClause, params ObjectParameter[] parameters) {
   return market.musteriler.Where(whereClause, parameters).AsEnumerable(); 
}



回答3:


I guess you need to add this at the beginning of your code:

using System.Linq;

It looks like your market is ObjectContext and musteriler is ObjectSet and it doesn't provide Where accepting lambda - you need extension Where method on IQueryable which accepts lambda expression.




回答4:


In the example I was trying , I was getting the error,

No overload for method 'Where' takes 3 arguments

with my where clause expression using SQLParametera.

I found that I needed

using System.Linq.Dynamic; once I discovered it, DUH



来源:https://stackoverflow.com/questions/11014017/dynamic-where-clauses-lambda-or-query-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!