SolvedPrivate

Another way of coding this loop

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

831 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