views:

40

answers:

2

I Use entity Framework 4.
I would like to be able to create a function that return an Expression func that will be use in a lambda expression.

  var ViewModel = _db.Suppliers.Select(model => new { 
                model,SupType = model.SupplierType.SupplierTypeTexts.Where( st => st.LangID == 1)   
            });

I would like to make this call like that

  var ViewModel = _db.Suppliers.Select(model => new { 
                model,SupType = model.SupplierType.GetText() 
            });

My Partial class is:

  public partial class SupplierType
    {

       public  Expression<Func<SupplierTypeText, bool>> GetText()
        {
            return p => p.LangID == 1;
        }

How can i perform this.

+1  A: 

Easy. For example, Let's assume you have a Product table that is mapped to Products EntitySet in your context, now you want to pass a predicate and select a Product:

Expression<Func<Product, bool>> GetPredicate(int id) {
    return (p => p.ProductID == id);
}

You can call GetPredicate() with a Product ID to filter based on that:

var query = ctx.Products.Where(GetPredicate(1)).First();

The point really is that you can always pass a Lambda Expression to where an Expression<T> is needed.

EDIT:
You should change your code like this:

var ViewModel = _db.Suppliers.Select(model => new { 
    model,
    SupType = model.SupplierType.SupplierTypeTexts.Where(GetText()) 
});
public Expression<Func<SupplierTypeText, bool>> GetText() {
    return (stt => stt.LangID == 1);
}
Morteza Manavi
Oh nice, I try your solution, but that gave me an error like that. LINQ to Entities does not recognize the method 'System.Linq.Expressions.Expression`1[System.Func`2[MySite.Models.Supplier,System.Boolean]] GetText()' method, and this method cannot be translated into a store expression.
Jean-Francois
So, your error is because of the GetText() method. Please share your code and I'll fix it up for you or let me know what is your exact scenario and I'll change my code accordingly.
Morteza Manavi
I made an update of my post. @Morteza Thanks for your support
Jean-Francois
It's a multilangual website, I want to get the text based on the client language choice. For now I hardcoded the language ID.
Jean-Francois
No problem. Ok, you cannot call GetText() on SupplierType, you should only pass GetText() when a predicate is required. Please have a look at my edited answer and let me know how it works for you.
Morteza Manavi
Where should I put the GetText() Method ?
Jean-Francois
It depends on your design, but this type of methods typically belong to the Repository class where you shape your queries. I can see that you put it in the SupplierType partial class which does not seems right to me unless you are following an "Active Record" design pattern. Did I answer your question?
Morteza Manavi
Yes absolutely.
Jean-Francois
Thanks for your help, it's working perfecly. And I put the get text in my repository where it's should be
Jean-Francois
You are very welcome, I'm glad it's working :)
Morteza Manavi
A: 

If you want to dynamically create compiled Expression at runtime (as opposed to ones hardcoded against a particular data model at compile time) you need to use the static methods on the Expression class.

marcind