Showing posts with label Extended Method. Show all posts
Showing posts with label Extended Method. Show all posts

Sunday, January 31, 2010

LINQ Extended method for Update Data

Extended Method

public delegate void Func(TArg0 element);

///
/// Executes an Update statement block on all elements in an IEnumerable sequence.
///
/// The source element type.
/// The source sequence.
/// The update statement to execute for each element.
/// The numer of records affected.
public static int Update(this IEnumerable source, Func update)
{
if (source == null) throw new ArgumentNullException("source");
if (update == null) throw new ArgumentNullException("update");
if (typeof(TSource).IsValueType)
throw new NotSupportedException("value type elements are not supported by update.");

int count = 0;
foreach (TSource element in source)
{
update(element);
count++;
}
return count;
}

Example :

int intUpdate = lstItem
.Where(a => a.ItemGroupId.Equals(lngItemGroupId))
.Update(b =>
{
b.GroupName = strGroupName;
b.GroupLocation = strLocation;
}
);