开发者

How to get next object in IEnumerable / IQueryable if ID key is not consistent?

After hacking more on my current app - once again, I am running into an issue which kills some of the joy I expected from my domain model.

The problem here is that the aggregate root / most important class in my domain model doesn't have consistent ID values for its entries. (E.g messed up : 1..3..5..12..150..157.. and so on | this is because it's a database that is being updated since MS Access 1997 and now upsized to MSSQLServer).

Is there a way to still get the real next object in the current collection ( which means that it is not just based on its ID value ) ?

What would you guys suggest me to do in this case?

And is it a good idea to:

  • De开发者_如何转开发lete ID field and regenerate it?
  • Create some HelperMethods to get the next object in the current? (How would you achieve that.. ?)


Are the IDs at least ordered, even if there are gaps? If so, simply order by the id and iterate over the enumeration. If you need access to the next object within a filter (Where for instance), you can use the extension method that provides access to the index in the collection.

Ex. 1:

 var query = db.Root.OrderBy( r => r.ID );
 foreach (var obj in query)
 {
    ....
 }

Ex. 2:

var query = db.Root.Where( (r,i) => db.Root.ElementAt(i).Name == r.Name );
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜