Solved

C# Replace Foreach loops with Linq

Posted on 2013-05-30
2
708 Views
Last Modified: 2013-09-02
How can I rewrite this query in Linq to get rid of the foreach loops:

IList<Charge> expenseItems = new List<Charge>();
foreach (ExpenseBatch batch in Query<ExpenseBatch>())
            {
                var batchItems = Query<ExpenseBatchItem>()
                                                  .Where(x => x.BatchId == batch.Id).ToList();
                                                                       
                foreach (var batchItem in batchItems)
                {
                    expenseItems.Add(
                        new Charge
                            {
                                Amount = batchItem.Amount,
                                Approved = batch.Approved,
                                Description = batchItem.Description,
                                BatchDate = batch.BatchDate,
                                BatchDescription = batch.BatchDescription,
                                Employee = batchItem.Employee,
                                ProjectCode = batch.ProjectCode
                            }
                        );
                }
            }
            return expenseItems .OrderBy(x => x.BatchDate).ToList();
0
Comment
Question by:ccravenbartle
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39207776
var expenseItems =
   (from batch in Query<ExpenseBatch>()
   join batchItem in Query<ExpenseBatchItem>() on batch.Id equals batchItem.BatchId
   select new Charge{ Amount = batchItem.Amount,
					Approved = batch.Approved,
					Description = batchItem.Description,
					BatchDate = batch.BatchDate,
					BatchDescription = batch.BatchDescription,
					Employee = batchItem.Employee,
					ProjectCode = batch.ProjectCode }).OrderBy(b=>b.BatchDate);

Open in new window

0
 

Author Closing Comment

by:ccravenbartle
ID: 39458480
Sorry for the delay in awarding the points
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 29
Chat Room 1 30
Help with creating multiple xml files from one xml file using VB.NET 6 25
Birthdays 3 22
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now