向集合添加索引

发布于 2024-10-21 19:28:03 字数 557 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下伊人醉 2024-10-28 19:28:03

不要为此使用 Linq:

int index = 0;
foreach (var model in TheOutputSoFar)
    model.TheIndex = index++;

Don't use Linq for that:

int index = 0;
foreach (var model in TheOutputSoFar)
    model.TheIndex = index++;
许仙没带伞 2024-10-28 19:28:03

TheIndex 属性设为 public

public class MyModel
{
  int prop1 {get;set;}
  int propn {get;set;}
  public int TheIndex {get;set;}
}

var outputDailyAppointments1 = TheOutputSoFar.Select((a, i) =>
    new ViewDailyAppointmentsModel{TheIndex = i});

但我不确定你为什么使用 TheOutputSoFar 来创建索引,你可以这样做

var outputDailyAppointments1 = Enumerable.Range(0, TheOutputSoFar.Count())
    .Select( i => new ViewDailyAppointmentsModel{TheIndex = i});

Make the TheIndex property public.

public class MyModel
{
  int prop1 {get;set;}
  int propn {get;set;}
  public int TheIndex {get;set;}
}

var outputDailyAppointments1 = TheOutputSoFar.Select((a, i) =>
    new ViewDailyAppointmentsModel{TheIndex = i});

But I'm not sure why you're using TheOutputSoFar to create indexes, you can do

var outputDailyAppointments1 = Enumerable.Range(0, TheOutputSoFar.Count())
    .Select( i => new ViewDailyAppointmentsModel{TheIndex = i});
长伴 2024-10-28 19:28:03

您应该小心构建代码的方式。 LINQ 应该用于对查询建模,而不是作为改变对象的方法。

您的代码(如发布的)有许多潜在的问题。例如, Select((a,i) => new ViewDailyAppointmentsModel{...} 的结果是一个查询。它实际上并没有改变您的集合...它将生成一个新集合每次评估时,原始集合不会反映任何更改。这实际上是您的意图吗?

如果 TheIndex 属性是瞬态的,并且仅在处理时需要。集合,考虑将其移出到作为查询的一部分构造的匿名类型中:

public class MyModel
{  
    int prop1 {get;set;}  
    int propn {get;set;}
}

var OutputDailyAppointments1 = TheOutputSoFar
   .Select((a, i) => new {Item = a, TheIndex = i}; // anonymous object captures index

另一方面,如果您实际上想要更新对象集,以便每个对象捕获其在列表中的最终位置,则只需使用循环(不要为此使用 LINQ..):

int position = 0;
foreach( var item in TheOutpuSoFar )
{
    item.TheIndex = position++;
}

您还应该考虑将项目的索引直接合并到其属性集中的设计选择是否是您想要的。在这样的模型中保持一致性通常很困难,但确实如此。很容易得到一个 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:

public class MyModel
{  
    int prop1 {get;set;}  
    int propn {get;set;}
}

var OutputDailyAppointments1 = TheOutputSoFar
   .Select((a, i) => new {Item = a, TheIndex = i}; // anonymous object captures index

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..):

int position = 0;
foreach( var item in TheOutpuSoFar )
{
    item.TheIndex = position++;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文