将值传递给函数

时间:2012-04-23 12:18:26

标签: c# linq

我想将filterType传递给GetPeronMemberDTO,如何实现这一点。

filters = filterItems.
    AsQueryable().
    Select(z => new Person()
    {
        Name = z.Name,
        ID = Convert.ToString(z.ID),
        FilterType = GetFilterType(Convert.ToString(z.TemplateID)),
        // TODO : How to pass the filtertype in the below function
        FilterMembers = GetPeronMemberDTO(
            Convert.ToString(z.ID), 
            searchParamDTO, 
            Convert.ToString(z.TemplateID)),
    })

由于 DS

2 个答案:

答案 0 :(得分:4)

使用查询语法,您可以使用let表达式:

filters = from z in filterItems.AsQueryable()
          let filterType = GetFilterType(Convert.ToString(z.TemplateID))
          select new Person
          {
            Name = z.Name,
            ID = Convert.ToString(z.ID),
            FilterType = filterType,
            FilterMembers = GetPeronMemberDTO(Convert.ToString(z.ID), 
              searchParamDTO, Convert.ToString(z.TemplateID), filterType)
          }

答案 1 :(得分:1)

filters = filterItems
          .AsQueryable()
          .Select(z => new {
                Item = z,
                FilterType = GetFilterType(Convert.ToString(z.TemplateID))
          })
          .Select(z => new Person()
            {
                Name = z.Item.Name,
                ID = Convert.ToString(z.Item.ID),
                FilterType = z.FilterType,
                FilterMembers = GetPeronMemberDTO(Convert.ToString(z.Item.ID), searchParamDTO, z.FilterType),
            })

或使用基于查询的语法,例如Anders Abels的答案。