Add empty element to list

c# entity-framework linq

Question

I have a list of

var Filials = Db.FILIALS.AsEnumerable().Where(x => x.PREFIX > 0).Select(x => new { Name = string.Format("{0} {1}", x.PREFIX, x.NAME), FilialId = (Guid?)x.FILIALID }).OrderBy(x => x.Name).ToList();

This list needs to include an empty element. I try this alternative

var lst = new[] { new { Name = string.Empty, FilialId = (Guid?)null } }.ToList();
var Filials = Db.FILIALS.AsEnumerable().Where(x => x.PREFIX > 0).Select(x => new { Name = string.Format("{0} {1}", x.PREFIX, x.NAME), FilialId = (Guid?)x.FILIALID }).OrderBy(x => x.Name).ToList();
lst = lst.Union(Filials);

Get error instead

Cannot implicitly convert type System.Collection.Generic.IEnumerable to System.Collection.Generic.List

the final line.

How should an element be added to a list correctly?

1
3
3/25/2013 11:33:29 AM

Accepted Answer

You should swap outToList() with AsEnumerable() in the sentence that readslst .

The difficulty is thatlst is the kindList<anonymous type> but Union returns IEnumerable<anonymous type> . AnIEnumerable<T> cannot be assigned to a type variableList<T> .

Using AsEnumerable() creates thelst a type of variableIEnumerable<anonymous type> .

2
3/25/2013 11:34:29 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow