为 lambda 表达式之外的变量赋值

发布于 2024-11-06 11:47:06 字数 543 浏览 0 评论 0原文

我想为位于 lambda 表达式外部的变量分配一个值。例如。

model.Categories =
   productService.GetAllCategories().Select(
      c => new CategoryViewModel 
      {
          CategoryId = c.CategoryId, 
          CategoryName = c.CategoryName, 
          IsSelected = c.CategoryId == cat 
          //how can i also assign the CategoryName to model.SelectedCategory?
      }).ToList();

model 还包含 SelectedCategory 的属性,如果 c.CategoryId == cat,我想将 CategoryName 分配给它代码>.我该怎么做?

I want to assign a value to a variable thats located outside of the lambda expression. For eg.

model.Categories =
   productService.GetAllCategories().Select(
      c => new CategoryViewModel 
      {
          CategoryId = c.CategoryId, 
          CategoryName = c.CategoryName, 
          IsSelected = c.CategoryId == cat 
          //how can i also assign the CategoryName to model.SelectedCategory?
      }).ToList();

model also contains a property of SelectedCategory, which I want to assign the CategoryName to it if c.CategoryId == cat. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

迷荒 2024-11-13 11:47:07

我认为在该查询中无法完成此操作。恐怕它必须在一个单独的查询中;像这样的东西(在分配model.Categories之后)

var selectedCategory = model.Categories.SingleOrDefault(c => c.IsSelected);
if(selectedCategory != null)
{ 
    model.SelectedCategory = selectCategory.CategoryName;
}

I don't think it can be done in that query. I'm afraid it is going to have to be in a seperate query; something like this (after model.Categories is assigned)

var selectedCategory = model.Categories.SingleOrDefault(c => c.IsSelected);
if(selectedCategory != null)
{ 
    model.SelectedCategory = selectCategory.CategoryName;
}
心碎的声音 2024-11-13 11:47:06

我并不热衷于这种编程风格,因为它很快就会变得不可读,但在某些情况下它可能很有用:

model.Categories =
    productService.GetAllCategories().Select(
        c =>
            {
                if (c.CategoryId == cat)
                    model.SelectedCategory = c.CategoryName;
                return new CategoryViewModel
                {
                    CategoryId = c.CategoryId,
                    CategoryName = c.CategoryName,
                    IsSelected = c.CategoryId == cat
                }
            }).ToList();

I'm not crazy about this style of programming as it quickly becomes unreadable but it can be useful in some situations:

model.Categories =
    productService.GetAllCategories().Select(
        c =>
            {
                if (c.CategoryId == cat)
                    model.SelectedCategory = c.CategoryName;
                return new CategoryViewModel
                {
                    CategoryId = c.CategoryId,
                    CategoryName = c.CategoryName,
                    IsSelected = c.CategoryId == cat
                }
            }).ToList();
山田美奈子 2024-11-13 11:47:06

之后我会做这样的事情:

model.SelectedCategory = model.Categories.Single(c => c.IsSelected).CategoryName;

不过理想情况下,我只是让 SelectedCategory 成为动态返回该属性的属性,而不是可能不同步的设置值:

public string SelectedCategory
{
    get 
    {
        Category selected = Categories.SingleOrDefault(c => c.IsSelected);
        return (selected != null ? selected.CategoryName : String.Empty);
    }
}

I'd just do something like this afterwards:

model.SelectedCategory = model.Categories.Single(c => c.IsSelected).CategoryName;

Ideally though, I'd just have SelectedCategory be a property dynamically returning that, rather than a set value that could fall out of sync:

public string SelectedCategory
{
    get 
    {
        Category selected = Categories.SingleOrDefault(c => c.IsSelected);
        return (selected != null ? selected.CategoryName : String.Empty);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文