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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:43:23+00:00 2026-05-16T12:43:23+00:00

I have replicated a stripped-down version of my code that has recently been re-written

  • 0

I have replicated a stripped-down version of my code that has recently been re-written to use linq to access the database.

However, in my opinion, the linq is really simple and could probably be optimized quite a bit, especially around line 90 where there is a linq statement inside a foreach loop.

It’d be really helpful to see how someone else would go about writing this simple task using linq. Thanks in advance! Below is a snippet of my source code.

    // Model objects - these are to be populated from the database,
// which has identical fields and table names.
public class Element
{
    public Element()
    {
        Translations = new Collection<Translation>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public Collection<Translation> Translations { get; set; }

    public class Translation
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public Language Lang { get; set; }
    }
}
public class Language
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
}

// Stripped-down functions for adding and loading Element
// objects to/from the database:

public static class DatabaseLoader
{
    // Add method isn't too bulky, but I'm sure it could be optimised somewhere.
    public static void Add(string name, Collection<Translation> translations)
    {
        using (var db = DataContextFactory.Create<ElementContext>())
        {
            var dbElement = new Database.Element()
            {
                Name = name
            };
            db.Elements.InsertOnSubmit(dbElement);
            // Must be submit so the dbElement gets it's Id set.
            db.SubmitChanges();

            foreach (var translation in translations)
            {
                db.Translations.InsertOnSubmit(
                    new Database.Translation()
                        {
                            FK_Element_Id = dbElement.Id,
                            FK_Language_Id = translation.Lang.Id,
                            Title = translation.Title,
                            Content = translation.Content
                        });
            }

            // Submit all the changes outside the loop.
            db.SubmitChanges();
        }
    }

    // This method is really bulky, and I'd like to see the many separate linq
    // calls merged into one clever statement if possible (?).
    public static Element Load(int id)
    {
        using (var db = DataContextFactory.Create<ElementContext>())
        {
            // Get the database object of the relavent element.
            var dbElement =
                (from e in db.Elements
                 where e.Id == id
                 select e).Single();

            // Find all the translations for the current element.
            var dbTranslations =
                from t in db.Translations
                where t.Fk_Element_Id == id
                select t;

            // This object will be used to buld the model object.
            var trans = new Collection<Translation>();

            foreach (var translation in dbTranslations)
            {
                // Build up the 'trans' variable for passing to model object.
                // Here there is a linq statement for EVERY itteration of the
                // foreach loop... not good (?).
                var dbLanguage =
                    (from l in db.Languages
                     where l.Id == translation.FK_Language_Id
                     select l).Single();

                trans.Add(new Translation()
                    {
                        Id = translation.Id,
                        Title = translation.Title,
                        Content = translation.Content,
                        Language = new Language()
                            {
                                Id = dbLanguage.Id,
                                Name = dbLanguage.Name, 
                                Code = dbLanguage.Code
                            }
                    });
            }

            // The model object is now build up from the database (finally).
            return new Element()
                {
                    Id = id,
                    Name = dbElement.Name,
                    Translations = trans
                };
        }
    }
}
  • 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-16T12:43:24+00:00Added an answer on May 16, 2026 at 12:43 pm

    Using some made-up constructors to oversimplify:

    public static Element Load(int id)
    {
        using (var db = DataContextFactory.Create<ElementContext>())
        {
            var translations = from t in db.Translations
                               where t.Fk_Element_Id == id
                               join l in db.Languages on t.FK_Language_Id equals l.Id
                               select new Translation(t, l);
    
            return new Element(db.Elements.Where(x => x.Id == id).Single(), translations);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have several ASP.NET applications that use a sitemap which is populated via a
I am using SQL Server 2000 and I have two databases that both replicate
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have a table (2 million rows) in Informix v11.10, replicated (50+ node) environment
I have an iPhone application that will save number of images in it. I
I have a DevExpress LookUpEdit that I am using withing Visual Studio 2008 in
I am running two erlang nodes with a replicated mnesia database. Whenever I tried
I have the following application which replicates an issue I'm having in a larger
I have 6 months of data, how do I replicate only the most current
Have just started using Google Chrome , and noticed in parts of our site,

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.