Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new anonymous type list to union onto existing anonymous type list

Tags:

c#

sql

linq

I have a list of anonymous types that I get from my database:

var takenChannels = (from b in bq.GetStuff(db)
                     where b.RecordType == "H" && b.TourStartDateTime.Date == date
                     select new { Start = b.TourStartDateTime, End = b.TourEndDateTime, Channel = b.RadioChannel, TourArea = b.TourArea }).ToList();

Then I use this list info to do some stuff in a foreach loop. I want to add to this list a new anonymous item for when I come back round in the loop.

Something like:

takenChannels.Union{new[] { new{Start = DateTime.Now, End = DateTime.Now.AddDays(1), Channel = 25, TourArea = "Area" }});

Obviously this doesn't work. How do I do it?

Edit 1:

takenChannels.Add(new { Start = s, End = e, Channel = channel, TourArea = booking.TourArea });

This is the closest I've got so far (Thanks to Daniel)... but the error I get is:

Error 6 Argument 1: cannot convert from 'AnonymousType#2' to 'AnonymousType#1'

like image 890
Smithy Avatar asked Jun 05 '13 19:06

Smithy


People also ask

Can we create nested anonymous type?

You are not allowed to create a field, property, event, or return type of a method is of anonymous type. You are not allowed to declare formal parameters of a method, property, constructor, indexer as a anonymous type. The scope of the anonymous type is always limited to the method in which they are defined.

How do you create an anonymous list object in Java?

You could do: var list = new[] { o, o1 }. ToList();

When you define an anonymous type the compiler converts that type into a?

The compiler generates a name for each anonymous type. If two or more anonymous type objects are defined in the same assembly and the sequence of properties are same in terms of names and types than the compiler treats both as same object instances of type.


2 Answers

This answer might be a bit late, but since this is the question I found when Googling for the same problem, I think I should complete it with a working answer.

There is no problem to Union multiple times over anonymous types. It is important that all properties are declared in all instances and that they have the same data type. if not, you get the error above.

  • In your specific case, does the database perhaps return TourStartDateTime or TourEndDateTime as DateTime??
  • Is RadioChannel an int from the database or perhaps an int? or string?
  • Is TourArea a string in the database?

Just make sure the data types match and you should be fine. Below is a working snippet of code I use in my own program:

var regions = (
    new[] { new { Id = "-1", Name = "---", Pattern = (string)null } }
  ).Union(
    from x in db.Userlists where x.ListType == 2 select new { Id = x.UserlistID.ToString(), Name = x.Name, Pattern = (string)null }
  ).Union(
    from x in db.Lookups where x.Category == "Stock" select new { Id = x.Key, Name = x.Key, Pattern = x.Value }
  ).ToArray();
like image 138
Mattias Åslund Avatar answered Sep 19 '22 02:09

Mattias Åslund


You can simply Add to the list:

takenChannels.Add(new { Start = ... });
like image 36
Daniel Hilgarth Avatar answered Sep 23 '22 02:09

Daniel Hilgarth