Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为lambda表达式之外的变量赋值_C#_Linq - Fatal编程技术网

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

C# 为lambda表达式之外的变量赋值,c#,linq,C#,Linq,我想给位于lambda表达式之外的变量赋值。例如 model.Categories = productService.GetAllCategories().Select( c => new CategoryViewModel { CategoryId = c.CategoryId, CategoryName = c.CategoryName, IsSelected = c.CategoryId

我想给位于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
分配给它。我如何做到这一点?

我认为在这个查询中无法做到这一点。我恐怕它将不得不在一个单独的查询;类似这样的内容(在分配了
model.Categories
之后)


我认为在那个问题上是做不到的。我恐怕它将不得不在一个单独的查询;类似这样的内容(在分配了
model.Categories
之后)


之后我会这样做:

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);
    }
}

之后我会这样做:

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);
    }
}

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

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();

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

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();

这将为所选类别分配类别对象;所需的结果是将CategoryName分配给SelectedCategory。@Metro啊!错过了那部分。固定的!这将为所选类别分配类别对象;所需的结果是将CategoryName分配给SelectedCategory。@Metro啊!错过了那部分。固定的!lambda可以被重构成一个新方法,比如
。选择(c=>GetViewModelForCategory(c))
,这样可以对转换逻辑进行单元测试+1lambda可以重构为一种新方法,如
。选择(c=>GetViewModelForCategory(c))
,它的好处是能够对转换逻辑进行单元测试+1.