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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:07:08+00:00 2026-06-18T08:07:08+00:00

I am trying to convert below nested for each loop into Linq. However I

  • 0

I am trying to convert below nested for each loop into Linq. However I am still unable to do it successfully.

var objAct = new List<InformaticsBenchmarkSummary>();
foreach (var item in op)
{
    foreach (var lstTp5 in lstTopFive)
    {
        if (item.UnitOfOperations.ContainsKey(lstTp5.SystemID))
        {
            var objIbm = new InformaticsBenchmarkSummary();
            objIbm.CompanyId = item.CompanyId;
            objIbm.CompanyName = item.CompanyName;
            objIbm.LocationId = item.LocationId;
            objIbm.LocationName = item.LocationName;
            objIbm.UnitOfOperations.Add(lstTp5.SystemID, 
                                        item.UnitOfOperations[lstTp5.SystemID]);
            objAct.Add(objIbm);
        }
    }
}

Where UnitOfOperations is of type Dictionary<int,string>();
op is again List<InformaticsBenchmarkSummary>()
lstTopFive is List<int>()

I tried, something like this but was unsuccessful syntactically

var output = from item in op
from lstTp5 in lstTopFive
where item.UnitOfOperations.ContainsKey(lstTp5.SystemID)
let v = new InformaticsBenchmarkSummary()
{
     CompanyId = item.CompanyId,
     CompanyName = item.CompanyName,
     LocationId = item.LocationId,
     LocationName = item.LocationName
}
.UnitOfOperations.Add(lstTp5.SystemID, item.UnitOfOperations[lstTp5.SystemID])
select v;

Nested loop works perfectly but I think, linq on this will increase performance.
Appreciate any help.

  • 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-18T08:07:09+00:00Added an answer on June 18, 2026 at 8:07 am

    It is impossible in a Linq query-syntax to use the UnitOfOperations.Add in the select. But you can do it using Method Chain and a SelectMany method:

    var objAcu = (op.SelectMany(item => lstTopFive, (item, lstTp5) => new { item, lstTp5 })  // <- Bad readability
                    .Where(t => t.item.UnitOfOperations.ContainsKey(t.lstTp5.SystemID))
                    .Select(t =>
                            {
                                var objIbm = new InformaticsBenchmarkSummary
                                {
                                    CompanyId = t.item.CompanyId,
                                    CompanyName = t.item.CompanyName,
                                    LocationId = t.item.LocationId,
                                    LocationName = t.item.LocationName
                                };
                                objIbm.UnitOfOperations.Add(t.lstTp5.SystemID, t.item.UnitOfOperations[t.lstTp5.SystemID]);
                                return objIbm;
                            })).ToList();
    

    If the property UnitOfOperations has a public set, in this case, you can use the query-syntax.

    How do you replace 1 foreach? By a from ... in ...

    Then, to replace 2 foreach, use 2 from ... in ...

    var objAct = (from item in op  // First foreach loop
                  from lstTp5 in lstTopFive  // Second foreach loop
                  where item.UnitOfOperations.ContainsKey(lstTp5.SystemID)
                  select new InformaticsBenchmarkSummary
                  {
                      CompanyId = item.CompanyId,
                      CompanyName = item.CompanyName,
                      LocationId = item.LocationId,
                      LocationName = item.LocationName,
                      UnitOfOperations = { { lstTp5.SystemID, item.UnitOfOperations[lstTp5.SystemID] } }
                  }).ToList();
    

    But I doubt it will increase performance in such an operation.

    Anyway, I don’t understand what you try to achieve. Because in all items of the output will have one and only one element in the dictionary UnitOfOperations. Is it really what you want to do?

    UPDATE

    List<int> systemIdTop5 = lstTopFive.Select(tp5 => tp5.SystemID).ToList();
    var objAct = (from item in op
                  select new InformaticsBenchmarkSummary
                  {
                      CompanyId = item.CompanyId,
                      CompanyName = item.CompanyName,
                      LocationId = item.LocationId,
                      LocationName = item.LocationName,
                      UnitOfOperations = systemIdTop5.Intersect(item.UnitOfOperations.Keys)
                                                     .ToDictionary(systemId => systemId, systemId => item.UnitOfOperations[systemId])
                  }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert the code below into a parallel loop. What is
I'm trying to convert the below bit of code into VB (and obviously apply
I am trying to convert an array of the RECT structure (given below) into
I am trying to convert a string of the below format dd/MM/yyyy hh:mm:ss.fff into
In the formula below I'm trying to convert a custom date string (yyyymmddhhmmss) into
I am trying to use apache-rewrite rule to convert the below URL: http://localhost/foo/bar/news.php?id=24 Into
I am trying to convert the below mentioned SQL query to LINQ query Qry
I have the below windsor statement and trying to convert it into a structuremap
I am trying to convert the code below to a shorthand linq, but not
I am trying to convert the below elasticsearch into pyes query and i could

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.