【问题标题】:LINQ Join Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition forLINQ Join Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“对象”不包含定义
【发布时间】:2016-05-26 08:54:31
【问题描述】:

我正在使用 EF6 并执行一些 LINQ 连接,然后将新结构传递给视图。问题是它会抛出Microsoft.CSharp.RuntimeBinder.RuntimeBinderException,因为这些新加入的structures are internal

C#

 var carMains = this.DatabaseManager.carClaims.Join(this.DatabaseManager.carConvictions, l => l.request_id, r => r.request_id, (l, r) => new { l.claim_amount, r.conviction_driver }).Take(10);
 return View("CarJoin", carMains.ToList());

查看

@model dynamic
@foreach (var m in Model)
{
    <br>
    <div>@(m.claim_amount ?? "")</div>
     <br>
    <div>@(m.conviction_driver ?? "")</div>
     <br>
}

我认为解决方案的方式是为每个连接创建对象并具有强类型视图,这将非常耗时,因为我们正在讨论具有 200 多个实体的多个数据库模型。

我确信现在已经有人遇到过这种情况,并且可能找到了一些耗时较少的解决方案。 如何将结构传递给视图而无需显式定义?

【问题讨论】:

    标签: c# linq join dynamic asp.net-mvc-5


    【解决方案1】:

    自己整理

    枚举后可以将其转换为ExpandoObject,然后视图将很高兴处理动态

    C#中的Expando扩展

    public static class ExtensionMethods
    {
        public static ExpandoObject ToExpando(this object obj)
        {
            IDictionary<string, object> expando = new ExpandoObject();
            foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(obj))
            {
                var value = propertyDescriptor.GetValue(obj);
                expando.Add(propertyDescriptor.Name, value == null || new[]
                {
                    typeof (Enum),
                    typeof (String),
                    typeof (Char),
                    typeof (Guid),
                    typeof (Boolean),
                    typeof (Byte),
                    typeof (Int16),
                    typeof (Int32),
                    typeof (Int64),
                    typeof (Single),
                    typeof (Double),
                    typeof (Decimal),
                    typeof (SByte),
                    typeof (UInt16),
                    typeof (UInt32),
                    typeof (UInt64),
                    typeof (DateTime),
                    typeof (DateTimeOffset),
                    typeof (TimeSpan),
                }.Any(oo => oo.IsInstanceOfType(value))
                    ? value
                    : value.ToExpando());
            }
    
            return (ExpandoObject)expando;
        }
    }
    

    然后查询会得到额外的.ToList().Select(o =&gt; o.ToExpando());,比如:

    var carMains =
                this.DatabaseManager.GetEntities<carClaims>()
                    .Join(this.DatabaseManager.GetEntities<carConvictions>(), l => l.request_id, r => r.request_id, (l, r) => new {l.claim_amount, r.conviction_driver})
                    .Take(10)
                    .ToList()
                    .Select(o => o.ToExpando());
            return View("CarJoin", carMains.ToList());
    

    查看代码完全没有变化。

    希望这可以为您节省一些时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2013-07-09
      相关资源
      最近更新 更多