How to create a List from an Entity Framework query result

c# c#-4.0 entity-framework

Question

How to Create a List from the Results of the Following Query

var  result = from c in sb.Swithches_SW_PanalComponents
                     select new { c.ID,c.SW_PanalComponents.ComponentsName,c.ComponentValue };
1
3
1/10/2012 3:18:10 AM

Accepted Answer

LAST EDIT

This is all you ever needed, based on your last statement.

List<Swithches_SW_PanalComponents> result = 
                                  sb.Swithches_SW_PanalComponents.ToList();

This, naturally, corresponds to

var result = sb.Swithches_SW_PanalComponents.ToList();

EDIT

According on your remarks, I believe this is what you desire:

List<SW_PanalComponents> result = sb.Swithches_SW_PanalComponents
                  .Select(c => new SW_PanalComponents { /* initialize your fields */ })
                  .ToList();

STOP EDIT

The ToList You want method, that's what. But think about using dot notation. It's considerably neater and trimmer for straightforward requests like this one.

var result = sb.Swithches_SW_PanalComponents
                  .Select(c => new { c.ID, c.SW_PanalComponents.ComponentsName, c.ComponentValue })
                  .ToList();

Additionally, keep in mind that you may use the function if all you need to do is iterate through your query and attempt to run it instantly.AsEnumerable()

var result = sb.Swithches_SW_PanalComponents
                  .Select(c => new { c.ID, c.SW_PanalComponents.ComponentsName, c.ComponentValue })
                  .AsEnumerable();

The benefit in this situation is that the kind of outcome is less defined.IEnumerablt<T> .

6
1/11/2012 1:21:31 AM

Popular Answer

akin to this

var  result =(from c in sb.Swithches_SW_PanalComponents
                     select new 
                     { c.ID,
                       c.SW_PanalComponents.ComponentsName,
                       c.ComponentValue 
                     }).ToList();


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