Not sure if I am asking this correctly (i am new to EF).
Basically here is my problem. I have a class called Cities, which is a really simple class but it maps my db table exactly
public class City
{
public int Id { get; set; }
public string Country { get; set; }
public string Province { get; set; }
public string CityName { get; set; }
public City()
{
// TODO: Add constructor logic here
}
}
Now later in my system i have a list of cities
public List Cities { get; private set; }
and a linq query as such
using (MYdbEntities myentity = new MYdbEntities())
{
var efCities = (from c in myentity.Cities
orderby c.CityName
select c);
Cities = efCities.ToList(); //this line croaks
}
So i get that the problem is that the linq returns a list of entities from the model, while Cities is a list of City objects which is different.
but how can i cast efCities into Cities (short of doing a horrible loop with my own mappings blech)
I google ad get bizarre stuff about T4 generated templates something something
pls dumb it down for me 🙂
I would like to keep my City object as simple as possible without any knowledge of Entity Framework because I plan to use it elsewhere
Thanks
If I understand you right, your cities class (model) is not the same at the one created by EF but has the same of similar propertie.
The simplest way based on what you have is to
Please excuse formatting, difficult on iPad.
Basically what you’re doing is creating new list of your City class mapping each property in the Select function.
You can alternatively use something like AutoMapper to map / populate your Cities class automatically. You can find AutoMapper here http://automapper.codeplex.com/.
Hope that helps
Sam