Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:03:17+00:00 2026-05-30T14:03:17+00:00

Not sure if I am asking this correctly (i am new to EF). Basically

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:03:18+00:00Added an answer on May 30, 2026 at 2:03 pm

    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

    var myCities = (efCities.Select(o=> new City() { 
       Id = o.Id, 
       Country = o.Country
       ....
     }).ToList();
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to C# and not sure if I'm asking this correctly. I
Im an iphone noob. I'm not sure if I'm asking this correctly, but I
I'm not sure if I'm even asking this question correctly. I just built my
Not sure if I am asking this correctly (e.g. using widget/object in the correct
Not sure if I am asking this correctly. But I am using a Jquery
I'm not sure I'm asking the right question here, but I'm looking to provide
I'm not sure whether I'm asking the question correctly, but I've been told SQL
Not sure if this is possible or if I'm expressing correctly what I'm looking
Not sure what's going on here. I have a DateTime object, and when I
I'll exemplify, since I'm not sure if I'm asking the question correctly (English is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.