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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:39:09+00:00 2026-05-30T23:39:09+00:00

I have a cluttery piece of code that I would like to shorten using

  • 0

I have a cluttery piece of code that I would like to shorten using Linq. It’s about the part in the foreach() loop that performs an additional grouping on the result set and builds a nested Dictionary.

Is this possible using a shorter Linq syntax?

        var q = from entity in this.Context.Entities
                join text in this.Context.Texts on new { ObjectType = 1, ObjectId = entity.EntityId} equals new { ObjectType = text.ObjectType, ObjectId = text.ObjectId}
        into texts
                select new {entity, texts};

        foreach (var result in q)
        {
            //Can this grouping be performed in the LINQ query above?
            var grouped = from tx in result.texts
                    group tx by tx.Language
                    into langGroup
                    select new
                               {
                                   langGroup.Key,
                                   langGroup
                               };
            //End grouping

            var byLanguage = grouped.ToDictionary(x => x.Key, x => x.langGroup.ToDictionary(y => y.PropertyName, y => y.Text));

            result.f.Apply(x => x.Texts = byLanguage);
        }

        return q.Select(x => x.entity);

Sideinfo:

What basically happens is that “texts” for every language and for every property for a certain objecttype (in this case hardcoded 1) are selected and grouped by language. A dictionary of dictionaries is created for every language and then for every property.

Entities have a property called Texts (the dictionary of dictionaries). Apply is a custom extension method which looks like this:

    public static T Apply<T>(this T subject, Action<T> action)
    {
        action(subject);
        return subject;
    }
  • 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-30T23:39:10+00:00Added an answer on May 30, 2026 at 11:39 pm

    isn’t this far simpler?

    foreach(var entity in Context.Entities)
    {
       // Create the result dictionary.
       entity.Texts = new Dictionary<Language,Dictionary<PropertyName,Text>>();
    
       // loop through each text we want to classify
       foreach(var text in Context.Texts.Where(t => t.ObjectType == 1 
                                                 && t.ObjectId == entity.ObjectId))
       {
           var language = text.Language;
           var property = text.PropertyName;
    
           // Create the sub-level dictionary, if required
           if (!entity.Texts.ContainsKey(language))
               entity.Texts[language] = new Dictionary<PropertyName,Text>();
    
           entity.Texts[language][property] = text;
       }
    }
    

    Sometimes good old foreach loops do the job much better.

    Language, PropertyName and Text have no type in your code, so I named my types after the names…

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

Sidebar

Related Questions

I have a twenty byte hex hash that I would like to store in
I have a do-while loop that needs to log a message once (so it
i have a Sharepoint feature that essentially extends Lists with a new feature, using
I'm using AspNetSqlMembershipProvider and created my pages for provisioning. Now that I have this
I have several applications that I wish to deploy using rpm. Some of the
I have a faceted plot (about which I had this other question). I would
Have just started using Google Chrome , and noticed in parts of our site,
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
in my class i have an array defined like this class t { var

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.