将字典滚动填充到 For 表达式的 LINQ 中
我有一个数据对象
Data = class
public
s: string;
i: Integer;
end;
,其中许多都在列表(或某个集合)中:
var ol : List<Data> := new List<Data>;
ol.Add(new Data(s := 'One', i := 1));
ol.Add(new Data(s := 'Two', i := 2));
ol.Add(new Data(s := 'Three', i := 3));
ol.Add(new Data(s := 'Four', i := 4));
ol.Add(new Data(s := 'Five', i := 5));
我想将其放入字典中:
var l: Dictionary<string, data> := new Dictionary<String,Data>;
我知道我可以执行 for 或 for every 循环来执行此操作
for each d in ol do
begin
l.Add(d.s, d);
end;
但我想使用 LINQ语句(首选)或 For 循环表达式。当只返回一个元素时很容易,但不确定字典的两个元素的语法。
I have a data object
Data = class
public
s: string;
i: Integer;
end;
with many of them in a list (or some collection):
var ol : List<Data> := new List<Data>;
ol.Add(new Data(s := 'One', i := 1));
ol.Add(new Data(s := 'Two', i := 2));
ol.Add(new Data(s := 'Three', i := 3));
ol.Add(new Data(s := 'Four', i := 4));
ol.Add(new Data(s := 'Five', i := 5));
And I want to put it in a dictionary:
var l: Dictionary<string, data> := new Dictionary<String,Data>;
I know I can do a for or for each loop to do this
for each d in ol do
begin
l.Add(d.s, d);
end;
But I want to either use a LINQ statement (preferred) or a For Loop expression. It is easy when only returning one element, but not sure on the syntax for the two elements for a Dictionary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:我直接跳进去,但我不熟悉德尔福。在 C# 中,它就像这个
ToDictionary
调用一样简单:我认为 Delphi 的等效项是:
EDIT: I jumped right into this but I'm not familiar with Delphi. In C# it would be as simple as this
ToDictionary
call:I think the Delphi equivalent would be: