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

Another way of coding this loop

Posted on 2014-12-03
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)

                    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;



Open in new window

Question by:Camillia
  • 2
  • 2
LVL 44

Expert Comment

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.

Author Comment

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

Accepted Solution

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.

Author Comment

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")

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Issue with SSIS module 26 103
linq, c# 8 22
asp.net mvc 2 22
I need help changing the default value for an @HTML.LabelFor control 4 14
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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