CloneExplicit<T>
Creates an explicit copy of the given enumerable where the only values copied are the ones you designate.
Source
/// <summary>
/// Creates an explicit clone assignment of the enumerable list
/// </summary>
/// <param name="obj">source enumeration</param>
/// <param name="attributeAssignment">Action that does the assignment
/// <para>First parameter is the new item</para>
/// <para>Second parameter is the source item</para></param>
/// <returns></returns>
public static IEnumerable<T> CloneExplicit<T>(this IEnumerable<T> obj, Action<T, T> assign) where T : new()
{
Func<T, Action<T>, T> chain = (x, y) =>
{
y(x);
return x;
};
return (from s in obj
select chain(new T(), t => assign(t, s))).ToList();
}
Example
IEnumerable<SelectListItem> YesNo = new Collection<SelectListItem>
{
new SelectListItem { Text = "Yes", Value = bool.TrueString, Selected = true },
new SelectListItem { Text = "No", Value = bool.FalseString },
};
var v = YesNo.CloneExplicit((t, s) => { t.Text = s.Text; t.Value = s.Value; });
Author: Daniel Gidman
Submitted on: 7 dec. 2010
Language: C#
Type: System.Collections.Generic.IEnumerable<T>
Views: 5250