我想在LINQ中做相同的以下内容,但我无法弄清楚如何:
IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
Run Code Online (Sandbox Code Playgroud)
什么是真正的语法?
受到另一个询问失踪问题的启发 Zip功能的:
为什么课堂上没有ForEach扩展方法Enumerable?还是在任 唯一获得ForEach方法的类是List<>.有没有理由错过(表演)?
我正在尝试找到一种配置AutoMapper的方法,以通过其源父对象的引用在目标对象中设置属性.下面的代码显示了我想要实现的目标.我正在从数据对象将数据移动到Parent&Child实例中.映射可以正常创建具有正确数据的List集合,但我需要有一个ForEach来分配父实例引用.
public class ParentChildMapper
{
public void MapData(ParentData parentData)
{
Mapper.CreateMap<ParentData, Parent>();
Mapper.CreateMap<ChildData, Child>();
//Populates both the Parent & List of Child objects:
var parent = Mapper.Map<ParentData, Parent>(parentData);
//Is there a way of doing this in AutoMapper?
foreach (var child in parent.Children)
{
child.Parent = parent;
}
//do other stuff with parent
}
}
public class Parent
{
public virtual string FamilyName { get; set; }
public virtual IList<Child> Children { get; set; }
}
public class Child
{
public …Run Code Online (Sandbox Code Playgroud) Rust语言是否有办法将函数应用于数组或向量中的每个元素?
我知道在Python中有map()执行此任务的功能.中的R存在lapply(),tapply()和apply()功能也做到这一点.
有没有一种既定的方法来渲染Rust中的函数?
假设我有这样的业务对象,
class Employee
{
public string name;
public int id;
public string desgination;
public int grade;
}
List<Employee> lstEmp = new List<Employee>()
{
new Employee() { name="A",desgination="SE",id=1},
new Employee() { name="b",desgination="TL",id=2},
new Employee() { name="c",desgination="PL",id=3},
new Employee() { name="d",desgination="SE",id=4},
new Employee() { name="e",desgination="SSE",id=5},
};
Run Code Online (Sandbox Code Playgroud)
如果我想将员工等级更新为3,其名称为"SE",那么我必须写这样的东西
lstEmp=lstEmp.Select(x =>
{
x.grade = (x.desgination == "SE") ? 3 : x.grade;
return x;
}).ToList();
Run Code Online (Sandbox Code Playgroud)
但是在使用select时,它会每次都生成新的employee对象,而不是更新现有的lstEmp,所以我必须将更新的列表重新分配给lstEmp.
在我看来,它经常更新大型更新时会影响性能.这有解决方法吗?
假设我有一组消息,其中包含"UserID"(int)和"Unread"(bool)属性.
对于UserID = 5的集合中的任何Message,如何使用LINQ扩展方法设置Unread = false?
所以,我知道我可以这样做:
messages.Any(m => m.UserID == 5);
Run Code Online (Sandbox Code Playgroud)
但是,如何使用扩展方法设置每个的Unread属性呢?
注意:我知道我不应该在生产代码中这样做.我只是想学习更多LINQ-fu.
有没有办法使用Linq执行以下操作:
foreach (var c in collection)
{
if (c.Condition == condition)
{
c.PropertyToSet = value;
// I must also check I only set this value to one minimum and only one element.
}
else
{
c.PropertyToSet = otherValue;
}
}
Run Code Online (Sandbox Code Playgroud)
为了澄清,我想迭代集合中的每个对象,然后更新每个对象的属性,除了我的集合中应该更新为另一个值的一个元素.
此时我使用计数器检查我将我的值设置为我的集合中的一个且仅一个元素.我从这个例子中删除了它,让人们建议其他解决方案.
没有收集例外原来的问题是在这里
编辑
我问这个问题是因为我不确定是否可以使用LinQ.所以你的回答让我对LinQ有了一些看法.谢谢.
因此,当您返回列表时,存储库会填充一个值,但另一个值要为列表中的每个项目具有相同的值.对于每个循环而言,感觉就像是用于简单功能的大量代码.有没有办法缩短代码.
所以有些背景.这是一个示例类.
public class ExampleClass
{
public string A { get; set; }
public string B { get; set;
}
Run Code Online (Sandbox Code Playgroud)
这是一种有效的方法:
public IEnumerable<ExampleClass> GetAll(string bValue)
{
var exampleList = repo.GetAll(); //Asume that the repo gives back the an list with A filled;
var returnValue = new List<ExampleClass>();
foreach (var item in exampleList)
{
item.B= bValue;
returnValue.Add(item);
}
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
如果有类似的东西会很棒:
public IEnumerable<ExampleClass> GetAll(string bValue)
{
return repo.GetAll().Map(i => i.B = bValue);
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这样的事情.
我正在尝试SELECT ALL使用 LINQ 向我的网格添加功能,但出现编译错误。
List<Person_> PeopleUpdated = People.ToList().ForEach(a => a.SELECTED = true).ToList();
Run Code Online (Sandbox Code Playgroud)
它说
无法将类型“void”隐式转换为“System.Collections.Generic.List < LogoSapHrIntegration.frmXmlUpload.Person_>”
我究竟做错了什么?
LINQ扩展List.ForEach()不返回任何内容(void),但我想为列表中的所有对象分配属性值,然后将其中一些返回为IEnumerable.
例如
return myCollection
.ForEach(x => x.ToBeDetermined = determine(x))
.Where(x => x.ToBeTermined == true);
Run Code Online (Sandbox Code Playgroud) 如何缩短以下代码,可能使用匿名方法或扩展名LINQ.
因为我必须多次重复这段代码,所以我希望尽可能简洁.
var imagesToUnlock = App.ImageListVM.Items.Where(img => img.Category == key);
foreach (var image in imagesToUnlock)
{
image.IsLocked = false;
}
Run Code Online (Sandbox Code Playgroud)