Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Another way of coding this loop

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

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
Avatar of Camillia

ASKER

I wanted to know if I can move that "if" into LINQ as well.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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")