将字典滚动填充到 For 表达式的 LINQ 中

发布于 2024-09-28 12:47:08 字数 811 浏览 0 评论 0原文

我有一个数据对象

  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 技术交流群。

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

发布评论

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

评论(1

呆橘 2024-10-05 12:47:08

编辑:我直接跳进去,但我不熟悉德尔福。在 C# 中,它就像这个 ToDictionary 调用一样简单:

var dict = ol.ToDictionary(o => o.s, o => o);

我认为 Delphi 的等效项是:

var dict := ol.ToDictionary(o -> o.s, o -> o);

EDIT: I jumped right into this but I'm not familiar with Delphi. In C# it would be as simple as this ToDictionary call:

var dict = ol.ToDictionary(o => o.s, o => o);

I think the Delphi equivalent would be:

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