具有两个对象的 Lambda 表达式
我知道这可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样:
Like this:
_values
是一个一开始就为空的List
吗?如果是这样,看起来可能如下所示:假设
_systemItem
和_institutionItem
是相同类型。如果它们是不相关的类型,您可能希望为它们提供一个定义LookupText
和LookupValue
(甚至是ToLookupValue
方法)的通用接口,并且然后将条件运算符中的操作数之一强制转换为接口。例如:Is
_values
aList<LookupValue>
which is empty to start with? If so, that look might look like this: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 definesLookupText
andLookupValue
(or even aToLookupValue
method) and then cast one of the operands in the conditional operator to the interface. For example:当然,我有意见。我会这样写:
并将
LookupValue
的对象初始值设定项移动到采用Institution
或System
的实际构造函数中。Sure, I have an opinion. I'd write it like this:
And move the object initializers for
LookupValue
into real constructors that take anInstitution
orSystem
.