在 LINQ 表达式中创建选定项时获取序号位置
在 LINQ 表达式中创建选定项目时如何获取序数位置?例如:
var alphaordinals = new string[] { "A", "B", "C", "D", "E" }; // etc.
var q = from car in datacontext.Cars
select new InventoryItem
{
Name = car.Name,
Price = car.Price,
UIElement = "Car " + car.Id.ToString() + ???
};
return q.ToList();
我希望汽车列表的 InventoryItem.UIElement 类似于:
Car 27 A
Car 28 B
Car 31 C
Car 48 D
等。
How can I get the ordinal position while creating selected Items in a LINQ expression? For example:
var alphaordinals = new string[] { "A", "B", "C", "D", "E" }; // etc.
var q = from car in datacontext.Cars
select new InventoryItem
{
Name = car.Name,
Price = car.Price,
UIElement = "Car " + car.Id.ToString() + ???
};
return q.ToList();
I want the InventoryItem.UIElement of the list of Cars to be like :
Car 27 A
Car 28 B
Car 31 C
Car 48 D
etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Select
的重载提供索引和值。在这种情况下,我将使用 AsEnumerable 退出数据库上下文,这并不是您真正想要做这项工作的地方:(您可以将 alphaOrdinals 更改为顺便说一句,没有对代码进行任何其他更改的字符串。)
Use the overload of
Select
which provides the index as well as the value. In this case I'd useAsEnumerable
to get out of the database context, which isn't really where you want to be doing this work:(You can change
alphaOrdinals
to a string without any other change to the code, btw.)