LINQ dynamic property in select

//Hello to all

I am making this call in action:

    [HttpGet]
    public virtual ActionResult JsonGetProvinces(int countryId)
    {
        //WebSiteContext WbContext = new WebSiteContext();
        //UnitOfWork UnitofWork = new UnitOfWork(WbContext);

        var provinces =
            (
                from province in unitofWork.ProvinceRepository.All
                where province.CountryId == countryId
                select new
                {
                    Id = province.Id,
                    Name = province.GetType().GetProperty("Name_" + CultureManager.GetCurrentCultureShortName()).GetValue(province)
                }
            ).ToList();

        return Json(provinces, JsonRequestBehavior.AllowGet);
    }

      

there is something wrong with my request:

        var provinces =
            (
                from province in unitofWork.ProvinceRepository.All
                where province.CountryId == countryId
                select new
                {
                    Id = province.Id,
                    Name = province.GetType().GetProperty("Name_" + CultureManager.GetCurrentCultureShortName()).GetValue(province)
                }
            ).ToList();

      

Particulary, Name = province.GetType (). GetProperty ("Name_" + CultureManager.GetCurrentCultureShortName ()). GetValue Province

In BDD has columns Name_fr

, Name_en

and I'm trying to make a dynamic ... Is it possible?

Of course I can take both and dynamically select the column in the view, but I would know how to do that ...

thanks for the help

+1


source to share


2 answers


EF cannot translate SQL function calls. Using expression trees can be tricky see this question

Here's an example with expression trees. GetQuery2 is the same as GetQuery, but with an expression tree and a property name parameter.



public static IQueryable<Foo> GetQuery(BlogContext context)
{
    var query = from x in context.BlogEntries
                select new Foo
                {
                    NameX = x.Name   
                };
    return query;
}


public static IQueryable<Foo> GetQuery2(BlogContext context, string propertyName)
{

    ConstructorInfo ci = typeof(Foo).GetConstructor(new Type[0]);
    MethodInfo miFooGetName = typeof(Foo).GetMethod("set_NameX");
    MethodInfo miBlogEntry = typeof(BlogEntry).GetMethod("get_" + propertyName);

    ParameterExpression param = Expression.Parameter(typeof(BlogEntry), "x");

    IQueryable<Foo> result = Queryable.Select<BlogEntry, Foo>(
                                context.BlogEntries,
                                Expression.Lambda<Func<BlogEntry, Foo>>(
                                    Expression.MemberInit(
                                        Expression.New(ci, new Expression[0]),
                                        new MemberBinding[]{
                                            Expression.Bind(miFooGetName, 
                                                            Expression.Property(param,
                                                            miBlogEntry))}
                                    ),
                                    param
                                )
                                );
    return result;
}

      

It's easier to get all all language strings and write an additional property name that does the magic.

+1


source


Short answer: you need to modify your code a bit and use an expression tree internally. Look at this question



+2


source







All Articles