使用 LINQ 根据结果将 IQueryable 中的所有值替换为字符串

发布于 2024-10-20 15:40:15 字数 350 浏览 1 评论 0原文

我有一个从数据库中选择的 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 技术交流群。

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

发布评论

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

评论(5

莫言歌 2024-10-27 15:40:15
var dictionary = new Dictionary<int, string>
{
    { 0, "Active" },
    { 1, "Inactive" },
    // etc
};

var AssessmentList = assessment                    
.Select(p => new LogManagementViewModel
{
    newValue = dictionary[p.NewValue],
});

这也适用于其他类型之间的映射。

var dictionary = new Dictionary<int, string>
{
    { 0, "Active" },
    { 1, "Inactive" },
    // etc
};

var AssessmentList = assessment                    
.Select(p => new LogManagementViewModel
{
    newValue = dictionary[p.NewValue],
});

This would work for mapping between other types as well.

橘虞初梦 2024-10-27 15:40:15
var values = new [] {"StrValue1", "StrValue2", "StrValue"};

var AssessmentList = assessment                    
    .AsEnumerable()
    .Select(p => new LogManagementViewModel
        {
            newValue = values[p.NewValue],
        });
var values = new [] {"StrValue1", "StrValue2", "StrValue"};

var AssessmentList = assessment                    
    .AsEnumerable()
    .Select(p => new LogManagementViewModel
        {
            newValue = values[p.NewValue],
        });
跨年 2024-10-27 15:40:15

最简单和最干净的方法是使用映射方法:

string MapToString(int value)
{
    //..
}

var AssessmentList = assessment                    
.Select(p => new LogManagementViewModel
{
   NewValue = MapToString(p.NewValue),
});

Easiest and cleanest would be with a mapping method:

string MapToString(int value)
{
    //..
}

var AssessmentList = assessment                    
.Select(p => new LogManagementViewModel
{
   NewValue = MapToString(p.NewValue),
});
吖咩 2024-10-27 15:40:15

一种方法:

将新的计算属性添加到您的类中(假设您在任何生成的代码之外都有一个分部类)。然后,该计算类可以根据需要进行转换。您的视图模型可以引用此计算值。

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.

寒江雪… 2024-10-27 15:40:15

我会在您的 LogManagementViewModel 中进行构造函数重载,这对您有用,如下所示:

public LogManagementViewModel(int NewValue)
{
  switch(NewValue)
  {
      case 0:
         this.NewValue = "Active";
         break;
       // etc...
  }
}

IMO,这将逻辑放在正确的位置,让您的视图模型按应有的方式翻译它,这不是您的 LINQ 的责任/数据库查询。

I would make a constructor overload in your LogManagementViewModel that does that work for you, something like this:

public LogManagementViewModel(int NewValue)
{
  switch(NewValue)
  {
      case 0:
         this.NewValue = "Active";
         break;
       // etc...
  }
}

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.

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