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

  • SEARCH
  • Home
  • 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 8221065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:46:04+00:00 2026-06-07T13:46:04+00:00

I am having some problem about how to work with an entity say an

  • 0

I am having some problem about how to work with an entity say an EF entity and a surrogate type, which will be bound to the UI.

Suppose that I have following classes

    // Db Entity
    public class Car
    {
        public virtual int Id { get; set; }
        public string ChassisNumber { get; set; }
        public virtual string Brand { get; set; }
        public virtual string Name { get; set; }
    }

    // Surrogate type that reflects some properties of Car entity
    // This class will be bound to UI
    public class SurrogateCar
    {
        public string Brand { get; set; }
        public string Name { get; set; }
    }

Now I will be getting List<Car> from db and want to create a List<SurrogateCar> that represents my entities. I can do this easily in many ways, one of them like this:

      List<Car> cars = CarTable.GetMyCars(); // Just a dummy method, suppose it returns all entities from Db.

      List<SurrogateCar> surrogates = new List<SurrogateCar>();

      foreach (var car in cars)
      {
           surrogates.Add(new SurrogateCar { Brand = car.Brand, Name = car.Name });
      }

or I can write a custom cast method. But what I worry about is the performance. This method will be called frequently, so creating a list and populating it one by one seems a potential problem to me.

Do you have any better ways to do this, or is it okay to use it like this?

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-06-07T13:46:05+00:00Added an answer on June 7, 2026 at 1:46 pm

    If you have a web service, and that service is always going to return the SurrogateCar class, then you can write your entity query to return the class you want rather than getting the class you don’t want:

    var cars = from c in context.Cars where {your condition}
               select new SurrogateCar
               {
                   Brand=c.Brand,
                   Name=c.Name
               };
    

    If, on the other hand you need the list of cars all the time, then as Roger pointed out AutoMapper is great! You just call

      CreateMap<Car, SurrogateCar>
    

    then you just use Automapper to populate your new list:

      surrogates.AddRange(Map<IEnumberable<Car>, IEnumerable<SurrogateCar>>(cars));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
My problem is specifically about some trouble I'm having parsing an Inkscape (XML) file,
I have a rather unique problem that I'm having trouble solving. I have a
I am having a problem with some javascript that I wrote that is only
I am having a problem with my website. I have some animations when a
I'm having a problem getting some code to work with the parallel package in
I'm having some problem with validations for multiple fields, specifically with case-sensitive unique validations
i am having some problem with position: absolute div. IE7 displays it next to
I'm having some problem with this statement declare @result int select @result = (select
I am having some problem with jsonp and jquery. This is my code -

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.