【问题标题】:LINQ dynamic property in select选择中的 LINQ 动态属性
【发布时间】:2012-12-16 00:41:06
【问题描述】:

//大家好

我在行动中做这个调用:

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

在 BDD 中,有 Name_frName_en 列 我正在尝试动态获取一个...有可能吗?

当然,我可以同时使用两者并在视图中动态选择列,但我想知道该怎么做...

感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

    简短的回答是您需要稍微更改代码并在内部使用表达式树。看看这个question

    【讨论】:

    • 使用 .net reflector(redgate 的一个工具),您可以反编译 LINQ 查询。使用 .net 2.0 配置文件很重要,因为 LINQ 不是 C# 的一部分。那么您可以轻松地在 select 语句中添加列。
    【解决方案2】:

    EF 无法将函数调用转换为 SQL。使用表达式树可能会很复杂see this question

    这是一个带有表达式树的示例。 GetQuery2 与 GetQuery 相同,但具有表达式树和属性名称参数。

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

    获取所有语言字符串并编写一个附加的属性 Name 会更容易。

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2013-05-07
      • 1970-01-01
      相关资源
      最近更新 更多