Camillia
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();
ASKER
I wanted to know if I can move that "if" into LINQ as well.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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")
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.