Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Setting all rows column value
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
LINQ
Title:
Setting all rows column value
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01622651
Message ID:
01622651
Views:
36
Hi everybody,

I am wondering if there is a simple method to set column's value to some value in a list of objects?

E.g. I have this code:
foreach (var subCategory in max4saleViewModel.subCategoriesList)
                    {
                        if (subCategory.IsBHold == true)
                        {
                            subCategory.StartTime = max4sale.StartTime;
                            subCategory.EndTime = max4sale.EndTime;

                            Max4Sale sbMax4Sale = AutoMapper.Mapper.Map<Max4SaleList, Max4Sale>(subCategory);

                            conflicts = GetConflicts(sbMax4Sale);
                            if (conflicts > 0)
                            {
                                throw ThrowConflictsError(conflicts);

                            }
                            sbMax4Sale.BHoldParId = max4sale.Id;
                            subCategoriesToAdd.Add(sbMax4Sale);
                        }
                        else
                        {
                            if (subCategory.Max4SaleId != 0)
                            {
                                subCategoriesToDelete.Add(AutoMapper.Mapper.Map<Max4SaleList, Max4Sale>(subCategory));
                            }
                        }
                    }
                }
                if (action=="Update")
                    _max4saleAdapter.Update(max4sale);
                else
                    _max4saleAdapter.Add(max4sale);
So, when I am updating an existing parent row, I can set BHoldParId column in the loop to a value. However, when I add a new parent row the id value will only be set after the Add has been run and I don't want to run another foreach loop if there is a simpler LINQ approach.

Thanks in advance.

UPDATE. Never mind, found an answer in StackOverflow:

http://stackoverflow.com/questions/1776387/using-linq-to-update-a-property-in-a-list-of-entities
 if (action=="Update")
                    _max4saleAdapter.Update(max4sale);
                else
                { 
                    _max4saleAdapter.Add(max4sale);
                    subCategoriesToAdd.ForEach(x=>x.BHoldParId=max4sale.Id);
                 }
If it's not broken, fix it until it is.


My Blog
Reply
Map
View

Click here to load this message in the networking platform