SolvedPrivate

Another way of coding this loop

Posted on 2014-12-03
4
55 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
  • 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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now