Solved

C# Replace Foreach loops with Linq

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

823 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