LINQ动态属性选择 [英] LINQ dynamic property in select

查看:232
本文介绍了LINQ动态属性选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//大家好,

我在行动中执行此呼叫:

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

我的查询有问题:

        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)

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

在BDD中有 Name_fr Name_en
,我想要动态使用一个...可以吗?

In BDD, there is Name_fr, Name_en columns and i'm trying to take one dynamically... Is it possible ?

当然,我可以同时选择并在View中动态地选择列,但我会知道如何...

Of course, i can take both and choose dynamically the column in View but i would to know how do...

感谢您的帮助

推荐答案

EF无法将函数调用转换为SQL。可以使用表达式树看到这个问题

EF can not translate function calls to SQL. Using expression trees can be comlicated see this question

这是一个表达式树的示例。 GetQuery2与GetQuery相同,但使用表达式树和一个propertyname参数。

Here is a sample with expression trees. The GetQuery2 is the same as GetQuery but with expression tree and a propertyname 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 is easier the fetch all all language strings and write an additional Property Name that does the magic.

这篇关于LINQ动态属性选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆