Solved

C# Replace Foreach loops with Linq

Posted on 2013-05-30
2
776 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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