C# LINQ Join返回显式类型而不是VAR

C# LINQ Join返回显式类型而不是VAR,c#,linq,C#,Linq,我有一个LINQ做我想要的工作 var query = context.MasterTemplateOfficeTag .Join(context.Tag, x => x.TagId, y => y.Id, (x, y) => new { y.Name }) .ToList(); 尽管我的问题是,我希望LINQ返回一个列表,因为Select语法=>new{y.Name}的类型是string。因此,如果编译器知道返回类型,为什

我有一个LINQ做我想要的工作

var query = context.MasterTemplateOfficeTag
             .Join(context.Tag, x => x.TagId, y => y.Id, (x, y) => new { y.Name })
             .ToList();
尽管我的问题是,我希望LINQ返回一个
列表
,因为Select语法
=>new{y.Name}
的类型是
string
。因此,如果编译器知道返回类型,为什么我不能使用
list

我想要这样的东西

 List<String> name = context.MasterTemplateOfficeTag
                      .Join(context.Tag, x => x.TagId, y => y.Id, (x, y) => new { y.Name })
                      .ToList();
List name=context.mastertemplateoffitag
.Join(context.Tag,x=>x.TagId,y=>y.Id,(x,y)=>new{y.Name})
.ToList();
这可能吗

谢谢

新建{y.Name})
使用
名称
属性创建匿名对象


只需返回
y.Name
即可使用
List

而不是返回匿名对象,只需返回字符串即可

List<String> name = context.MasterTemplateOfficeTag
                      .Join(context.Tag, x => x.TagId, y => y.Id, (x, y) => y.Name)
                      .ToList();
List name=context.mastertemplateoffitag
.Join(context.Tag,x=>x.TagId,y=>y.Id,(x,y)=>y.Name)
.ToList();

是具有单个
字符串
字段(
名称
)的匿名对象。拖放
新{…}
包装并返回
字符串

   List<String> name = context
     .MasterTemplateOfficeTag
     .Join(
         context.Tag, 
         x => x.TagId, 
         y => y.Id, 
        (x, y) => y.Name ) // <- Now we return string: y.Name
     .ToList();
列表名称=上下文
.MasterTemplateOfficeTag
.加入(
context.Tag,
x=>x.TagId,
y=>y.Id,

(x,y)=>y.Name)/
(x,y)=>y.Name
:拖放
{..}
试试看,我们可以知道它是否可能。visual studio的妙处在于,当您在工作示例中将鼠标悬停在
var
上时,它会显示返回的类型。我有一种感觉,他们可能不是你选择作为OP的sameIt,但我建议你接受Dmitry的答案,因为它更加充实:)
   List<String> name = context
     .MasterTemplateOfficeTag
     .Join(
         context.Tag, 
         x => x.TagId, 
         y => y.Id, 
        (x, y) => y.Name ) // <- Now we return string: y.Name
     .ToList();