Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Replace Foreach loops with Linq

Posted on 2013-05-30
2
Medium Priority
?
916 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:
Meir Rivkin earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 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