【问题标题】:Object doesn't have definition for servingTime [duplicate]对象没有服务时间的定义[重复]
【发布时间】:2019-02-13 21:07:50
【问题描述】:

我有一个匿名类型列表List<'a>, a' is new {OptionSetValue servingTime, OptionSetValue servingGroup, string servingClass, string desc, string childName, Guid childId, EntityReference item, OptionSetValue type, Guid sensitivity}

我在 foreach 语句中构建了这样一个列表,需要从 for 循环 (groupedAllergies) 外部将该列表添加到另一个相同类型的列表中。显然,我写的内容不起作用,因为 object 不包含我试图访问的匿名类型的定义。我该如何补救?!

    var groupedAllergies = new List<object>(); <--- problem

    foreach (var line in items)
    {
        OptionSetValue servingTime = line.servingTime;
        OptionSetValue servingGroup = line.servingGroup;
        string servingClass = line.servingClass;
        string desc = line.desc;
        string childName = line.childName;
        Guid childId = line.childId;
        Guid sensitivity = line.sensitivity;


         var queryAllergyItems = groupAllergies.Where(e => e.servingClass == servingClass
                                                          && e.servingTime == servingTime
                                                          && e.servingGroup == servingGroup
                                                          && e.desc == desc
                                                          && e.allergies.Intersect(allergies).Any()).Select(r => new
                                                          {
                                                              r.item,
                                                              r.type,
                                                              r.allergies
                                                          }).ToList();

         var items = queryAllergyItems.Select(r => new
         {
             servingTime,
             servingGroup,
             servingClass,
             desc,
             childName,
             childId,
             r.item,
             r.type,
             sensitivity = r.allergies.Intersect(allergies).First()


          }).ToList();

          items.ForEach(e => groupedAllergies.Add(e));

    }



     foreach(var allergy in groupedAllergies)
     {
          var servingTime = allergy.servingTime;  
          //object doesn't have definition for servingTime!!

     }

这是一个有效的解决方案吗?!

var o = new { servingTime = new OptionSetValue(), servingGroup = new OptionSetValue(), servingClass = "", desc = "", childName = "", childId = new Guid(), item = new EntityReference(), type = new OptionSetValue(), sensitivity = new Guid() };

var groupedAllergies = new[] { o }.ToList();
groupedAllergies.Clear();

【问题讨论】:

  • 如果您在同一个程序集 (/DLL/project) 中并创建了一个具有相同属性、相同类型、与列表中对象顺序相同的对象,那么它将类型与列表中的对象兼容。如果你说var l = (from x in xyz select new {x.anInt, y.aString}).ToList();,你应该可以说var item = new {anInt = 5, aString = "s"}; l.Add(item);AddRange() 也是如此
  • 创建匿名类型列表(只要完全匹配)是完全可以的(如我所相信的duplicate 所示)。没有minimal reproducible example(包括实际错误),很难看出你有什么问题。
  • 这真的是“通用列表”问题的重复吗?感觉不像。无论如何,基于我之前的评论的答案有效(我打算发布它,但是当我离开会议时,它已被标记为 dup)。答案使用AddRange 将一个匿名对象列表扩展为另一个对象。

标签: c# linq


【解决方案1】:

试试这个:

List<dynamic> list = new List<dynamic>();

这将创建一个可以存储任何类型对象的列表。

【讨论】:

    【解决方案2】:

    如果您使用的是 C# 7,您实际上并不需要匿名类型。您可以使用值元组。它们基本上是具有特殊语言支持的元组。

    https://blogs.msdn.microsoft.com/mazhou/2017/05/26/c-7-series-part-1-value-tuples/


    至于List&lt;dynamic&gt;,这取决于,如果你的类型相同,动态变得非常快。不过并不比静态类型快。

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2019-08-22
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多