Search code examples
c#.netlinqentity

How to combine multiple fields into one field using LINQ


I have an Entity table with 2 int fields and want to get all the values into a list of ints not a list of the combinations of ints

This doesn't seem to give a list of single ints

var allItems = (from tbl1 in objContext.MyTable
select new { tbl1.Field1, tbl1.Field2 }).ToList();

How can I achieve this?


Solution

  • You should be able to use:

    var allItems = objContext.MyTable
                    .SelectMany(t => new[] { t.Field1, t.Field2 })
                    .ToList();
    

    The first select creates an array from the two elements, then the SelectMany flattens that into a single enumerable.