views:

317

answers:

5

I have a list of objects and I'd like to update a particular member variable within one of the objects. I understand LINQ is designed for query and not meant to update lists of immutable data. What would be the best way to accomplish this? I do not need to use LINQ for the solution if it is not most efficient.

Would creating an Update extension method work? If so how would I go about doing that?

EXAMPLE:
(from trade in CrudeBalancedList
 where trade.Date.Month == monthIndex
 select trade).Update(
 trade => trade.Buy += optionQty);
+2  A: 

Although linq is not meant to update lists of immutable data, it is very handy for getting the items that you want to update. I think for you this would be:

(from trade in CrudeBalancedList
    where trade.Date.Month == monthIndex
    select trade).ToList().ForEach( trade => trade.Buy += optionQty);
Patrick Karcher
+1  A: 

So you're expecting to get a single result here. In that case you might consider utilizing the SingleOrDefault method:

var record =
    (from trade in CrudeBalancedList
    where trade.Date.Month == monthIndex
    select trade).SingleOrDefault();

if (record != null)
    record.Buy += optionQty;

Note that the SingleOrDefault method expects there to be exactly one or zero value returned (much like a row in a table for some unique primary key). If more than one record is returned, the method will throw an exception.

Dan Tao
A: 

To create such a method, you would start with its prototype:

public static class UpdateEx {
    public void Update(this IEnumerable<T> items, 
                       Expression<Action> updateAction) {
    }
}

That's the easy part.

The hard part will be to compile the Expression<Action> into an SQL update statement. Depending on how much syntax you want to support, such a compiler's complexity can range from trivial to impossible.

For an example of compiling Linq Expressions, see the TableQuery class of the sqlite-net project.

Frank Krueger
A: 

I'm not sure if this is the best way, but will allow you to update an element from the list.

The test object:

 public class SomeClass {
        public int Value { get; set; }
        public DateTime Date { get; set; }
    }

The extension method:

public static class Extension {
        public static void Update<T>(this T item, Action<T> updateAction) {
            updateAction(item);
        }
    }

The test:

public void Test()
{
    // test data
    List<SomeClass> list = new List<SomeClass>()
    {
        new SomeClass {Value = 1, Date = DateTime.Now.AddDays(-1)},
        new SomeClass {Value = 2, Date = DateTime.Now },
        new SomeClass {Value = 3, Date = DateTime.Now.AddDays(1)}
    };
    // query and update
    (from i in list where i.Date.Day.Equals(DateTime.Now.Day) select i).First().Update(v => v.Value += 5);

    foreach (SomeClass s in list) {
        Console.WriteLine(s.Value);
    }
}
Fernando
A: 

I saw this entry and felt linking to it would be appropriate for anyone looking my question up.

http://stackoverflow.com/questions/1344140/extension-method-for-update-in-linq-to-objects

Addie