CRUD like operations in LINQ

Linq is wonderful for my integer programming application. It has eliminated the need for a backend DB, reduced tremendously the amount of code, and the execution time has been cut in half.

I am wondering if there is a simpler way of doing CRUD like operations in Linq, especially Updates and Deletes on a list (Shifts in this case) like we do in SQL

Delete From Shifts Where ShiftID IN 1,2,3

Update Shifts Set NumPeople=10 Where ShiftD=21

I am accomplishing this currently by iterating through the list.
[1541 byte] By [sami] at [2008-2-19]
# 1
While I don't think there is anything built in yet, it would be easy enough to do with an extension method. For example, something like this:



public static void Delete<T>(this List<T> list /* I forget if you need list or not */, Func<bool, T> selector)
{
// Iterate through the list. Iterate backwards so that
// We don't get confused with the index.
for (int index = list.Count - 1; index >= 0; --index)
{
// If the selector says it should be deleted, then remove it.
if (selector(list[index]))
{
// Remove the item.
list.RemoveAt(index);
}
}
}


Then, on your list, you could do something like:



List<Shift> list = ...;

// Call the extension method.
list.Delete(c => c.ShiftID == 1 || c.ShiftID == 2 || c.ShiftID == 3);

Hope this helps.

- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

casperOne at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 2
Perhaps this?



public static void Delete<T>(this List<T> list, Func<bool, T> selector)
{
list.Where(selector).ForEach(item => list.Remove(item));
}

.. assuming the output of Where() has ForEach available to it. I'm unable to check at this point.

I'm pretty certain you'd need list in the params. Remember that extension methods are static methods, which are sugared onto objects of the first parameter type to make them appear as if they were non-static methods.

KeithFarmer at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 3
Keith,

I don't know that this would work. The reason for this is that ForEach takes IEnumerable<T>. As a result, it iterates over the items in the enumeration. I also believe that you can not modify a list while enumerating over it (which is why I used the for loop instead of foreach).

- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

casperOne at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 4
Try it out and let me know -- I am curious. I'm not sure the list modification strictly applies since we're looping over the results of the Where generator, rather than list directly. It may be that you need to insert a reverse between the where and the foreach, though.

This may be a place to create a modified Where(), which performs the filter, but returns a new list.

List<T> has ForEach(Action<T>) as a member, FYI.

(I wish LINQ had a 64-bit distribution...)

KeithFarmer at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 5
List<T> (the new generic list collection introduced in .NET 2.0) has a RemoveAll(Predicate<T>) method which deletes all items for which the Predicate<T> function returns true. For example, to remove all numbers outside the range 0..99:



List<int> numbers = ...;
numbers.RemoveAll(x => x < 0 || x > 99);

Of course, the lambda syntax only works with C# 3.0.

Anders

AndersHejlsberg at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 6
Anders Hejlsberg wrote:
List<T> (the new generic list collection introduced in .NET 2.0) has a RemoveAll(Predicate<T>) method which deletes all items for which the Predicate<T> function returns true. For example, to remove all numbers outside the range 0..99:



List<int> numbers = ...;
numbers.RemoveAll(x => x < 0 || x > 99);

Of course, the lambda syntax only works with C# 3.0.

Anders

And here I thought I was being innovative =)

Good to see you in the forums.

- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

casperOne at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...

Visual Studio Orcas

Site Classified