c# - C# 如何在 LINQ 查询中访问 SelectListItem 的 Selected 属性的值?
问题描述
我有以下 LINQ 语句,我想知道如何string.Equals
在运行时查看运算符中的参数值。谁能告诉我如何设置我的代码,也许使用 avar
来做到这一点?
Items = from Item in GetList()
select new SelectListItem
{
Selected = string.Equals(Item.Value, formTypeSelected, StringComparison.IgnoreCase),
Text = Item.Key,
Value = Item.Value
};
解决方案
这是因为查询返回的执行IEnumerable
被推迟了,所以除非你请求数据,否则你不会得到这么多,例如当你转换它时ToList<T>
:
Items = (from Item in GetList()
select new SelectListItem
{
Selected = string.Equals(Item.Value, formTypeSelected, StringComparison.IgnoreCase),
Text = Item.Key,
Value = Item.Value
}).ToList();
推荐阅读
- python - 如何在 Kivy 中动态更改标签背景颜色
- python - Python中数据类嵌套列表的装饰器
- amazon - 当我在 Amazon Mechanical Turk 中公开批处理(上传 csv 文件)时,它总是在我最后一次数据观察后为该行注入错误?
- rest - 如何使用 REST API 创建消费者以从 Kafka 主题读取 json 数据
- logging - 仅将连续标准输出的最后一行保存到文件中
- magento - 跨平台验证问题:链接在某些设备上不起作用
- android - 运行时 android 依赖项是否被编译到可部署存档中?(apk/aar)
- r - 使用 sjplot 的 plot_model 函数时调整构面顺序和图例标签
- apache-camel - 为了监控目的,是否需要在 Camel 上进行任何预设置以公开 Camel Routes 的 MBean?
- javascript - 如何使用 JavaScript 从“数据”属性中读取数据