?
SolvedPrivate

Another way of coding this loop

Posted on 2014-12-03
4
Medium Priority
?
79 Views
Last Modified: 2016-02-17
This works but I use foreach. Is there a better way of coding this?

 public bool Update(IList<FulfillmentModel> model)
            {

                try
                {
                    foreach (var f in model)
                    {
                        if (f.Usertype == "C")
                        {
                            var consumer =
                                _dataContext.Consumers.Where(m => m.Id == f.Id)
                                    .SingleOrDefault(m => m.ConfirmationCode == f.ConfirmationCode);
                              if (consumer != null) consumer.SubmissionStatusId = (int) StatusSubmission.Status.Fulfilled;
                        }

                      
                    }
                

                _dataContext.SubmitChanges();

Open in new window

0
Comment
Question by:Camillia
[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
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40480153
If it works then one might criticise the code but is it bad?  Code that works is pretty useful.  
There are alternatives, eg code something to find (collections often have a function such as Find that you can supply an override to) the items based on the selection BUT then the loop just goes from your code to the built in loop in the background that uses that function.  Is that better?


ps.  One can keep searching for the base of the rainbow where some legends say a pot of gold is kept.  I've never heard of one actually finding it though.
0
 
LVL 7

Author Comment

by:Camillia
ID: 40480491
I wanted to know if I can move that "if" into LINQ as well.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 40480556
I'm not a great user of LINQ but instead of
_dataContext.Consumers.Where(m => m.Id == f.Id)
how about
_dataContext.Consumers.Where(m => ((m.Id == f.Id) && (f.Usertype == "C")) )
so the first if statement isn't required.


ps.  I doubt it has any effect on performance where exactly this filter is used.

pps.  Things like this I'd try before asking a question.  If it works - a lot less time and effort taken from your side, if it doesn't work then you can still ask a question.
0
 
LVL 7

Author Comment

by:Camillia
ID: 40480594
pps.  Things like this I'd try before asking a question.  If it works - a lot less time and effort taken from your side, if it doesn't work then you can still ask a question.

I did actually and googled too. It was midnight when  I was still working and needed to get some stuff done. It's ok, tho.

I tried it the wrong way, however.. I did (m.id == f.id && f.usertype == "c")
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 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