选择LINQ动态属性

时间:2012-12-16 00:41:06

标签: asp.net asp.net-mvc entity-framework linq-to-objects

//大家好

我在行动中做了这个电话:

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

格外, Name = province.GetType()。GetProperty(“Name_”+ CultureManager.GetCurrentCultureShortName())。GetValue(省)

在BDD中,有Name_frName_en列 我试图动态地采取一个......这可能吗?

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

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

简短的回答是你需要稍微改变你的代码并在里面使用表达式树。看看这个question

答案 1 :(得分:1)

EF无法将函数调用转换为SQL。使用表达式树可以复制see this question

以下是带有表达式树的示例。 GetQuery2与GetQuery相同,但使用表达式树和propertyname参数。

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

获取所有语言字符串更容易,并编写一个额外的属性名称来实现神奇。