lom*_*axx 460 .net c# linq foreach
有没有办法使用LINQ执行以下操作?
foreach (var c in collection)
{
c.PropertyToSet = value;
}
Run Code Online (Sandbox Code Playgroud)
为了澄清,我想迭代集合中的每个对象,然后更新每个对象的属性.
我的用例是我在博客文章中有一堆评论,我想在博客文章中迭代每个评论,并将博客帖子上的日期时间设置为+10小时.我可以在SQL中完成它,但我想将它保留在业务层中.
Cam*_*and 789
虽然您可以使用ForEach扩展方法,但如果您只想使用框架,则可以使用
collection.Select(c => {c.PropertyToSet = value; return c;}).ToList();
Run Code Online (Sandbox Code Playgroud)
该ToList是必要的,以评估立即选择由于懒惰的评估.
Ε Г*_*И О 321
collection.ToList().ForEach(c => c.PropertyToSet = value);
Run Code Online (Sandbox Code Playgroud)
Rah*_*hul 67
我这样做
Collection.All(c => { c.needsChange = value; return true; });
Run Code Online (Sandbox Code Playgroud)
lom*_*axx 27
我实际上找到了一个扩展方法,可以很好地完成我想要的操作
public static IEnumerable<T> ForEach<T>(
this IEnumerable<T> source,
Action<T> act)
{
foreach (T element in source) act(element);
return source;
}
Run Code Online (Sandbox Code Playgroud)
Hen*_*ish 13
使用:
ListOfStuff.Where(w => w.Thing == value).ToList().ForEach(f => f.OtherThing = vauleForNewOtherThing);
Run Code Online (Sandbox Code Playgroud)
我不确定这是否过度使用LINQ,但是当想要更新列表中特定条件的特定项时,它对我有用.
Him*_*ere 13
尽管您特别要求提供 LINQ 解决方案并且这个问题已经很老了,但我还是发布了一个非 LINQ 解决方案。这是因为 LINQ(= 语言集成查询)旨在用于对集合的查询。所有 LINQ 方法都不会修改底层集合,它们只是返回一个新集合(或者更准确地说是一个新集合的迭代器)。因此,无论您做什么,例如使用 aSelect都不会影响基础集合,您只需获得一个新集合。
当然,您可以使用 a ForEach(顺便说一下,这不是 LINQ,而是 上的扩展List<T>)。但这字面上foreach无论如何都可以使用,但是带有 lambda 表达式。除此之外,每个LINQ 方法都会在内部迭代您的集合,例如使用foreach或for,但是它只是对客户端隐藏它。我不认为这更具可读性和可维护性(考虑在调试包含 lambda 表达式的方法时编辑代码)。
话虽如此,这不应该使用 LINQ 来修改您的集合中的项目。更好的方法是您在问题中已经提供的解决方案。使用经典循环,您可以轻松迭代您的集合并更新其项目。事实上,所有依赖的解决方案List.ForEach都没有什么不同,但从我的角度来看更难阅读。
因此,在要更新集合元素的情况下,不应使用 LINQ 。
没有内置的扩展方法来执行此操作.虽然定义一个是相当直接的.在帖子的底部是我定义的一个名为Iterate的方法.它可以像这样使用
collection.Iterate(c => { c.PropertyToSet = value;} );
Run Code Online (Sandbox Code Playgroud)
迭代源
public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T> callback)
{
if (enumerable == null)
{
throw new ArgumentNullException("enumerable");
}
IterateHelper(enumerable, (x, i) => callback(x));
}
public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
{
if (enumerable == null)
{
throw new ArgumentNullException("enumerable");
}
IterateHelper(enumerable, callback);
}
private static void IterateHelper<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
{
int count = 0;
foreach (var cur in enumerable)
{
callback(cur, count);
count++;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些变化,我会继续回到这个人的解决方案.
http://www.hookedonlinq.com/UpdateOperator.ashx
再次,这是别人的解决方案.但我已经将代码编译成一个小型库,并且经常使用它.
我将在这里粘贴他的代码,因为他的网站(博客)将来某个时候不再存在.(没有什么比看到帖子上写着"这是你需要的确切答案",点击和死网更糟糕了.)
public static class UpdateExtensions {
public delegate void Func<TArg0>(TArg0 element);
/// <summary>
/// Executes an Update statement block on all elements in an IEnumerable<T> sequence.
/// </summary>
/// <typeparam name="TSource">The source element type.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="update">The update statement to execute for each element.</param>
/// <returns>The numer of records affected.</returns>
public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> 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;
}
}
int count = drawingObjects
.Where(d => d.IsSelected && d.Color == Colors.Blue)
.Update(e => { e.Color = Color.Red; e.Selected = false; } );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
462446 次 |
| 最近记录: |