-2

I have a List of anonymous types List<'a>, a' is new {OptionSetValue servingTime, OptionSetValue servingGroup, string servingClass, string desc, string childName, Guid childId, EntityReference item, OptionSetValue type, Guid sensitivity}

I have constructed such a list inside a foreach statement and need to add the list to another list of the same type from outside the for loop (groupedAllergies). Obviously what I wrote would not work as object does not contain a definition for the anonymous types I am trying to access. How can I remedy this?!

    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!!

     }

Is this a valid solution?!

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();
Alan Judi
  • 1,074
  • 1
  • 16
  • 32
  • If you are in the same assembly (/DLL/project) and you create an object with the same properties, of the same type, in the same order as the objects in the list, then it will be type compatible with the objects in the list. If you say `var l = (from x in xyz select new {x.anInt, y.aString}).ToList();`, you should be able to say `var item = new {anInt = 5, aString = "s"}; l.Add(item);`. The same goes for `AddRange()` – Flydog57 Feb 13 '19 at 21:15
  • It is perfectly fine (as shown in what I believe [duplicate](https://stackoverflow.com/questions/612689/a-generic-list-of-anonymous-class)) to create list of anonymous types (as long as there is exact match). Without [MCVE] (including actual errors) it is hard to see what you have problem with. – Alexei Levenkov Feb 13 '19 at 21:32
  • Is this really a dup of the "Generic List" question? It doesn't feel like it. Anyways, an answer based on my earlier comment works (I was going to post it, but when I got out of a meeting, it had been marked as a dup). The answer used `AddRange` to extend one list of anonymous objects with the objects in another. – Flydog57 Feb 13 '19 at 22:11

2 Answers2

0

Try this:

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

this creates a list which could store any type of object.

Mohammed
  • 396
  • 4
  • 15
0

If you are using C# 7 you don't really need anonymous types. you can use value tuples. they are basically tuples with special language support.

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


As for List<dynamic>, it depends, if your types are same, dynamic becomes very fast. not faster than static types though.

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118