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 9256939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:05:42+00:00 2026-06-18T12:05:42+00:00

So we’ve got two methods shown below: Method 1 private IEnumerable<object> CreateCentreViewModelForExport(IQueryable<CentreTranslation> centreTranslation) {

  • 0

So we’ve got two methods shown below:

Method 1

private IEnumerable<object> CreateCentreViewModelForExport(IQueryable<CentreTranslation> centreTranslation)
{
    return centreTranslation.Select(s => new
    {
        id = s.Centre.id,
        centreTranslationId = s.id,
        name = s.Centre.name,
        number = s.Centre.number,
        date_opened = s.Centre.date_opened,
        address_line_1 = s.address_line_1,
        address_line_2 = s.address_line_2,
        address_line_3 = s.address_line_3,
        city = s.city,
        county = s.county,
        country = s.Centre.Country.name,
        //country_id = s.Centre.country_id,
        translatedCountry = s.country,
        postcode = s.postcode,
        hidden = !(s.Centre.CentreStatus.Where(w => w.environment_id == 4).FirstOrDefault().active),
        about = s.about,
        virtualTour = s.Centre.virtual_tour,
        directions = s.directions,
        phone = s.Centre.phone,
        fax = s.Centre.fax,
        email = s.Centre.email,
        lat = s.Centre.position.Latitude,
        lng = s.Centre.position.Longitude,
        imageCount = s.Centre.image_count,
        translatedCentreName = s.name,
        amenities = s.amenities ,
        features = s.FeatureTranslations.Select(s2 => new FeatureViewModel()
        {
            id = s2.id,
            name = s2.Feature.name,
            selected = s2.selected
        }),
        businessCentreAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessCentre).FirstOrDefault().about,
        officeSpaceAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.OfficeSpace).FirstOrDefault().about,
        virtualOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.VirtualOffice).FirstOrDefault().about,
        meetingRoomsAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.MeetingRooms).FirstOrDefault().about,
        businessLoungeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessLounge).FirstOrDefault().about,
        dayOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.DayOffice).FirstOrDefault().about,
        language_group = s.Language.language_group,
        culture = s.Language.cuture
    });
}

Method 2

private IQueryable<CentreViewModel> CreateCentreViewModel(IQueryable<CentreTranslation> centreTranslation)
{
    return centreTranslation.Select(s => new CentreViewModel()
    {
        id = s.Centre.id,
        centreTranslationId = s.id,
        name = s.Centre.name,
        number = s.Centre.number,
        date_opened = s.Centre.date_opened,
        address_line_1 = s.address_line_1,
        address_line_2 = s.address_line_2,
        address_line_3 = s.address_line_3,
        city = s.city,
        county = s.county,
        //country = s.Centre.Country.name,
        country_id = s.Centre.country_id,
        translatedCountry = s.country,
        postcode = s.postcode,
        hidden = !(s.Centre.CentreStatus.Where(w => w.environment_id == 4).FirstOrDefault().active),
        about = s.about,
        virtualTour = s.Centre.virtual_tour,
        directions = s.directions,
        phone = s.Centre.phone,
        fax = s.Centre.fax,
        email = s.Centre.email,
        lat = s.Centre.position.Latitude,
        lng = s.Centre.position.Longitude,
        imageCount = s.Centre.image_count,
        translatedCentreName = s.name,
        amenities = s.amenities,
        features = s.FeatureTranslations.Select(s2 => new FeatureViewModel()
        {
            id = s2.id,
            name = s2.Feature.name,
            selected = s2.selected
        }),
        businessCentreAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessCentre).FirstOrDefault().about,
        officeSpaceAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.OfficeSpace).FirstOrDefault().about,
        virtualOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.VirtualOffice).FirstOrDefault().about,
        meetingRoomsAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.MeetingRooms).FirstOrDefault().about,
        businessLoungeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessLounge).FirstOrDefault().about,
        dayOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.DayOffice).FirstOrDefault().about
    });
}

As can be seen there is a lot of duplicate code. The second method returns a strongly typed view model, while the first returns an object due to the inclusion of two extra properties (language_group and culture).

The second method is used to populate an MVC view, the second for an export to Excel function.
What’s the best way of re factoring this to minimize the duplication?

  • 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-18T12:05:43+00:00Added an answer on June 18, 2026 at 12:05 pm

    Create a static FromCentreTranslation method in the CentreViewModel class, and put all initialization there:

    public class CentreViewModel 
    {
       ....
       public static CentreViewModel FromCentreTranslation(CentreTranslation source)
       {
          CentreViewModel result = new CentreViewModel();
          result.id = source.Centre.id,
          result.centreTranslationId = source.id,
          result.name = source.Centre.name,
          ....
          result.businessLoungeAbout = source.ProductTranslations
                            .Where(w => w.Product.id == (int)Products.BusinessLounge)
                            .FirstOrDefault().about,
          result.dayOfficeAbout = source.ProductTranslations
                            .Where(w => w.Product.id == (int)Products.DayOffice)
                            .FirstOrDefault().about
          return result;
       }
    }
    

    You can then refactor the original two methods like:

    private IEnumerable<object> CreateCentreViewModelForExport
                                    (IQueryable<CentreTranslation> centreTranslation)
    {
       return centreTranslation.Select(s => new 
       {
          centreViewModel = CentreViewModel.FromCentreTranslation(s),
          language_group = s.Language.language_group,
          culture = s.Language.cuture
       }
    }
    

    and

    private IQueryable<CentreViewModel> CreateCentreViewModel
                                    (IQueryable<CentreTranslation> centreTranslation)
    {
      return centreTranslation.Select(s => CentreViewModel.FromCentreTranslation(s)),
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.