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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:24:32+00:00 2026-05-19T02:24:32+00:00

I’ve written the following code to set the properties on various classes. It works,

  • 0

I’ve written the following code to set the properties on various classes. It works, but one of my new year’s rsolutions is to make as much use of LINQ as possible and obviously this code doesn’t. Is there a way to rewrite it in a “pure LINQ” format, preferably without using the foreach loops? (Even better if it can be done in a single LINQ statement – substatements are fine.)

I tried playing around with join but that didn’t get me anywhere, hence I’m asking for an answer to this question – preferably without an explanation, as I’d prefer to “decompile” the solution to figure out how it works. (As you can probably guess I’m currently a lot better at reading LINQ than writing it, but I intend to change that…)

 public void PopulateBlueprints(IEnumerable<Blueprint> blueprints)
 {
   XElement items = GetItems();
   // item id => name mappings
   var itemsDictionary = (
     from item in items
     select new
     {
       Id = Convert.ToUInt32(item.Attribute("id").Value),
       Name = item.Attribute("name").Value,
     }).Distinct().ToDictionary(pair => pair.Id, pair => pair.Name);

  foreach (var blueprint in blueprints)
  {
    foreach (var material in blueprint.Input.Keys)
    {
      if (itemsDictionary.ContainsKey(material.Id))
      {
        material.Name = itemsDictionary[material.Id];
      }
      else
      {
        Console.WriteLine("m: " + material.Id);
      }
    }

    if (itemsDictionary.ContainsKey(blueprint.Output.Id))
    {
      blueprint.Output.Name = itemsDictionary[blueprint.Output.Id];
    }
    else
    {
      Console.WriteLine("b: " + blueprint.Output.Id);
    }
  }
}

Definition of the requisite classes follow; they are merely containers for data and I’ve stripped out all the bits irrelevant to my question:

public class Material
{
  public uint Id { get; set; }

  public string Name { get; set; }
}

public class Product
{
  public uint Id { get; set; }

  public string Name { get; set; }
}

public class Blueprint
{
  public IDictionary<Material, uint> Input { get; set; }

  public Product Output { get; set; }
}
  • 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-19T02:24:33+00:00Added an answer on May 19, 2026 at 2:24 am

    I don’t think this is actually a good candidate for conversion to LINQ – at least not in its current form.

    Yes, you have a nested foreach loop – but you’re doing something else in the top-level foreach loop, so it’s not the easy-to-convert form which just contains nesting.

    More importantly, the body of your code is all about side-effects, whether that’s writing to the console or changing the values within the objects you’ve found. LINQ is great when you’ve got a complicated query and you want to loop over that to act on each item in turn, possibly with side-effects… but your queries aren’t really complicated, so you wouldn’t get much benefit.

    One thing you could do is give Blueprint and Product a common interface containing Id and Name. Then you could write a single method to update the products and blueprints via itemsDictionary based on a query for each:

    UpdateNames(itemsDictionary, blueprints);
    UpdateNames(itemsDictionary, blueprints.SelectMany(x => x.Input.Keys));
    
    ...
    
    private static void UpdateNames<TSource>(Dictionary<string, string> idMap,
        IEnumerable<TSource> source) where TSource : INameAndId
    {
        foreach (TSource item in source)
        {
            string name;
            if (idMap.TryGetValue(item.Id, out name))
            {
                item.Name = name;
            }
        }
    }
    

    This is assuming you don’t actually need the console output. If you do, you could always pass in the appropriate prefix and add an “else” block in the method. Note that I’ve used TryGetValue instead of performing two lookups on the dictionary for each iteration.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.