具有两个对象的 Lambda 表达式

发布于 2024-10-15 22:41:23 字数 958 浏览 1 评论 0原文

我知道这可以使用 lambda 表达式重写。但我似乎无法弄清楚。有人对如何使用 lambda 编写它有意见吗?

            foreach (var _systemItem in _systemData)
            {
                foreach (var _institutionItem in _institutionData)
                {
                    if (_systemItem.LookupValue == _institutionItem.OriginalSystemLookupValue)
                    {
                        _values.Add(new LookupValue()
                        {
                            DisplayText = _institutionItem.LookupText,
                            Value = _institutionItem.LookupValue
                        });
                    }
                    else
                    {
                        _values.Add(new LookupValue()
                        {
                            DisplayText = _systemItem.LookupText,
                            Value = _systemItem.LookupValue
                        });
                    }
                }
            }

I know that this can be rewritten using a lambda expression. But I cant seem to figure it out. does anyone have an opinion on how it should be written using a lambda.

            foreach (var _systemItem in _systemData)
            {
                foreach (var _institutionItem in _institutionData)
                {
                    if (_systemItem.LookupValue == _institutionItem.OriginalSystemLookupValue)
                    {
                        _values.Add(new LookupValue()
                        {
                            DisplayText = _institutionItem.LookupText,
                            Value = _institutionItem.LookupValue
                        });
                    }
                    else
                    {
                        _values.Add(new LookupValue()
                        {
                            DisplayText = _systemItem.LookupText,
                            Value = _systemItem.LookupValue
                        });
                    }
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

飘然心甜 2024-10-22 22:41:23

像这样:

values.AddRange(from s in _systemData
                from i in institutionData
                select s.LookupValue == i.OriginalSystemLookupValue ?
                    new LookupValue {
                        DisplayText = _institutionItem.LookupText,
                        Value = _institutionItem.LookupValue
                    }
                :   new LookupValue {
                        DisplayText = _systemItem.LookupText,
                        Value = _systemItem.LookupValue
                    }
               );

Like this:

values.AddRange(from s in _systemData
                from i in institutionData
                select s.LookupValue == i.OriginalSystemLookupValue ?
                    new LookupValue {
                        DisplayText = _institutionItem.LookupText,
                        Value = _institutionItem.LookupValue
                    }
                :   new LookupValue {
                        DisplayText = _systemItem.LookupText,
                        Value = _systemItem.LookupValue
                    }
               );
明明#如月 2024-10-22 22:41:23

_values 是一个一开始就为空的 List 吗?如果是这样,看起来可能如下所示:

_values = (from x in _systemData
           from y in _institutionData
           let item = x.LookupValue == y.OriginalSystemLookupValue ? x : y
           select new LookupValue { DisplayText = item.LookupText,
                                    Value = item.LookupValue })
          .ToList();

假设 _systemItem_institutionItem 是相同类型。如果它们是不相关的类型,您可能希望为它们提供一个定义 LookupTextLookupValue (甚至是 ToLookupValue 方法)的通用接口,并且然后将条件运算符中的操作数之一强制转换为接口。例如:

_values = (from x in _systemData
           from y in _institutionData
           let item = x.LookupValue == y.OriginalSystemLookupValue
                      ? (ILookupSource) x : y
           select item.ToLookupValue())
          .ToList();

Is _values a List<LookupValue> which is empty to start with? If so, that look might look like this:

_values = (from x in _systemData
           from y in _institutionData
           let item = x.LookupValue == y.OriginalSystemLookupValue ? x : y
           select new LookupValue { DisplayText = item.LookupText,
                                    Value = item.LookupValue })
          .ToList();

That assumes that _systemItem and _institutionItem are the same type. If they're unrelated types, you might want to give them a common interface that defines LookupText and LookupValue (or even a ToLookupValue method) and then cast one of the operands in the conditional operator to the interface. For example:

_values = (from x in _systemData
           from y in _institutionData
           let item = x.LookupValue == y.OriginalSystemLookupValue
                      ? (ILookupSource) x : y
           select item.ToLookupValue())
          .ToList();
浪漫人生路 2024-10-22 22:41:23

当然,我有意见。我会这样写:

var pairs = _systemData.SelectMany(s =>
    _institutionData.Select(i => new { System = s, Institution = i }));

_values.AddRange(pairs.Select(x =>
{
    bool match = x.System.LookupValue == x.Insitution.OriginalSystemLookupValue;

    return match ? new LookupValue(x.Institution) : new LookupValue(x.System);
}));

并将 LookupValue 的对象初始值设定项移动到采用 InstitutionSystem 的实际构造函数中。

Sure, I have an opinion. I'd write it like this:

var pairs = _systemData.SelectMany(s =>
    _institutionData.Select(i => new { System = s, Institution = i }));

_values.AddRange(pairs.Select(x =>
{
    bool match = x.System.LookupValue == x.Insitution.OriginalSystemLookupValue;

    return match ? new LookupValue(x.Institution) : new LookupValue(x.System);
}));

And move the object initializers for LookupValue into real constructors that take an Institution or System.

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