开发者

Lambda expression with two objects

开发者 https://www.devze.com 2023-02-08 11:18 出处:网络
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.

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 = _开发者_JS百科institutionItem.LookupValue
                        });
                    }
                    else
                    {
                        _values.Add(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
                    }
               );


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();


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.

0

精彩评论

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