Reputation: 85
im using entity framewok and who have a problem.
when i get data,i dont use return type a model class.so who have created a class and entity framework returns type of my class as below:
List<MixedArticle> lstMxa=new List<MixedArticle>();
Model.BlogDBEntities bdbe = new Model.BlogDBEntities();
SqlParameter sp = new SqlParameter("@count", count);
object[] parameters = new object[1] { sp };
lstMxa = bdbe.Database.SqlQuery<Facade.MixedArticle>("select * from fn_GetLastXArticles(@count)", parameters).ToList();
but i can use toList method as like
lstMxa=bdbe.Articles...where(x=x.Count==count).ToList<Facade.MixedArticle>();
when i tried this way visual studio rejects and says that it was not TSource.
so,how do i use ToList() method?
Upvotes: 0
Views: 802
Reputation: 177133
You can use a projection that selects the properties you have in MixedArticle
from the Articles
table in the database:
lstMxa = bdbe.Articles
.Where(x => x.Count == count)
.Select(x => new Facade.MixedArticle
{
SomePropertyInMixedArticle1 = x.SomeProperty1,
SomePropertyInMixedArticle2 = x.SomeProperty2,
// etc.
})
.ToList();
You could also load the full Article
entity from the database and then map the needed properties over to MixedArticle
(for example using a tool like AutoMapper). But the benefit of a projection with Select
is that it doesn't load more column values from the database than you actually need in MixedArticle
- but at the expense that you have to list and assign all those properties manually in the Select
expression.
Upvotes: 2