向集合添加索引
我有一个 List 集合中的对象 MyObjects 列表。
对象模型类似于:
public class MyModel
{
int prop1 {get;set;}
int propn {get;set;}
int TheIndex {get;set;}
}
我完成了一些 linq-to-objects 查询,最终得到 var TheOutputSoFar = ....
TheOutputSoFar 包含 MyModel 列表,但集合中的每个项目的 TheIndex = 0。我正在寻找一种方法,使集合中的每个项目的 TheIndex 递增 1。这是我尝试过的,但不起作用:
var OutputDailyAppointments1 = from a in TheOutputSoFar
.Select(a => a).AsEnumerable().Select((a, i) =>
new ViewDailyAppointmentsModel{TheIndex = i};
有什么建议吗? 谢谢。
I have a list of objects MyObjects in a List collection.
The object model is something like:
public class MyModel
{
int prop1 {get;set;}
int propn {get;set;}
int TheIndex {get;set;}
}
I finish a few linq-to-objects queries and I end up with
var TheOutputSoFar = ....
TheOutputSoFar contains the list of MyModel but each item in the collection has TheIndex = 0. I'm looking for a way to make TheIndex increment by 1 in each item of the collection. This is what I tried but it's not working:
var OutputDailyAppointments1 = from a in TheOutputSoFar
.Select(a => a).AsEnumerable().Select((a, i) =>
new ViewDailyAppointmentsModel{TheIndex = i};
Any suggestions?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要为此使用 Linq:
Don't use Linq for that:
将
TheIndex
属性设为public
。但我不确定你为什么使用 TheOutputSoFar 来创建索引,你可以这样做
Make the
TheIndex
propertypublic
.But I'm not sure why you're using TheOutputSoFar to create indexes, you can do
您应该小心构建代码的方式。 LINQ 应该用于对查询建模,而不是作为改变对象的方法。
您的代码(如发布的)有许多潜在的问题。例如,
Select((a,i) => new ViewDailyAppointmentsModel{...}
的结果是一个查询。它实际上并没有改变您的集合...它将生成一个新集合每次评估时,原始集合不会反映任何更改。这实际上是您的意图吗?如果
TheIndex
属性是瞬态的,并且仅在处理时需要。集合,考虑将其移出到作为查询的一部分构造的匿名类型中:另一方面,如果您实际上想要更新对象集,以便每个对象捕获其在列表中的最终位置,则只需使用循环(不要为此使用 LINQ..):
您还应该考虑将项目的索引直接合并到其属性集中的设计选择是否是您想要的。在这样的模型中保持一致性通常很困难,但确实如此。很容易得到一个 Index 属性无法正确反映其意图的集合,这里需要避免许多潜在的陷阱 - 例如,如果您使用
OrderBy
等方法对此类项目进行重新排序,则会产生混乱。 。You should be careful about how you structure your code. LINQ should be used to model queries, not as a means to mutate objects.
Your code (as posted) has a number of potential problems. For instance, the result of
Select((a,i) => new ViewDailyAppointmentsModel{...}
is a query. It is not actually mutating your collection ... it will produce a new collection of model objects each time it is evaluated. The original collection will not reflect any changes. Is this actually your intent?If the
TheIndex
property is transient, and only needed when processing the collection, consider moving it out into an anonymous type constructed as part of the query:If, on the other hand, you actually want to update your set of objects so each captures its final position in the list, then just use a loop (don't use LINQ for this..):
You should also consider whether the design choice of incorporating the index of an item directly into it's set of properties is what you want. It's generally difficult to maintain consistency in such a model, and it's easy to end up with a collection where the Index property does not correctly reflect its intent. There are a number of potential traps to avoid here - such as confusion if you ever use methods like
OrderBy
to reorder such items.