1

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
  • market is my database which connected datamodel. and i defined it as private AkilliMarketEntities market; Commented Jun 13, 2012 at 12:04

4 Answers 4

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

similar error Error 1 The best overloaded method match for 'System.Data.Objects.ObjectQuery<AkilliMarket.musteriler>.Where(string, params System.Data.Objects.ObjectParameter[])' has some invalid arguments Error 2 Argument 1: cannot convert from 'System.Func<musteriler,bool>' to 'string'
Do you have using System.Linq like @Ladislav suggested?
yes I have... my all references are using System; using System.Collections.Generic; using System.Linq; using System.Text;
no again... Error 2 Argument 2: cannot convert from 'System.Func<musteriler,bool>' to 'System.Func<AkilliMarket.musteriler,int,bool>' Error 1 'System.Collections.Generic.IEnumerable<AkilliMarket.musteriler>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,bool>)' has some invalid arguments
Do not use AsEnumerable() here, use AsQueryable(). AsEnumerable will cause the whole table to load into memory, before applying the "Where()".
|
1
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 Comments

thank for your nice answer but I get same error. because the problem is not about return type.. it shows problem is in predicate.. and same error which I post in my first message
using System; using System.Collections.Generic; using System.Linq; using System.Text;
thank you Raphael, your second code works too :) thank you very much for your interesting..
0

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.

1 Comment

yest market is an objectcontext.. and could you give me a sample for it ?
-1

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

1 Comment

This has nothing to do with the question asked here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.