使用 LINQ 根据结果将 IQueryable 中的所有值替换为字符串
我有一个从数据库中选择的 IQueryable,其中包含一个名为“NewValue”的字段。该字段将包含 0、1 或 2。我想用基于数字的字符串替换该值。
即 0 =“活动”。我知道 IQueryables 不用于更改值,主要只是为了查询,但我需要在下一行中使用它来添加到 ViewModel
var AssessmentList = assessment
.Select(p => new LogManagementViewModel
{
newValue = p.NewValue,
});
如何将 asset.NewValue 中的所有值更改为字符串?
I have an IQueryable I selected from a database that contains a field called "NewValue". This field will either contain a 0, 1, or 2. I want to replace the value with a string based of which number it is.
i.e 0 = "Active". I know IQueryables aren't used to change values mostly just for querying but I need to use it in the next line to add to the ViewModel
var AssessmentList = assessment
.Select(p => new LogManagementViewModel
{
newValue = p.NewValue,
});
How can I change all the values in assement.NewValue to a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这也适用于其他类型之间的映射。
This would work for mapping between other types as well.
最简单和最干净的方法是使用映射方法:
Easiest and cleanest would be with a mapping method:
一种方法:
将新的计算属性添加到您的类中(假设您在任何生成的代码之外都有一个分部类)。然后,该计算类可以根据需要进行转换。您的视图模型可以引用此计算值。
One way:
Add a new computed property onto your class (assuming you have a partial class outside of any generated code). Then that computed class can do the translation however it needs to. Your view model can reference this computed value.
我会在您的 LogManagementViewModel 中进行构造函数重载,这对您有用,如下所示:
IMO,这将逻辑放在正确的位置,让您的视图模型按应有的方式翻译它,这不是您的 LINQ 的责任/数据库查询。
I would make a constructor overload in your LogManagementViewModel that does that work for you, something like this:
IMO, this places the logic in it's proper place, let the your view model translate it as it should be, it's not the responsibility of your LINQ/Database queries.