Solved

C# Replace Foreach loops with Linq

Posted on 2013-05-30
2
824 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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