Dynamic where clauses lambda or query in C#

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?

Answers


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

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(); 
}

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.


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


Need Your Help

Android app icon name picked from activity label than from application label

android android-manifest

Not sure if anyone else posted this question, didn't find any, though there are similar ones.

Read referenced namespaces from Type

c# reflection namespaces

I need a way to check out all namespaces used by a type via reflection.