C# Linq-如何收集匿名类型作为函数的结果

C# Linq-如何收集匿名类型作为函数的结果,c#,linq,entity-framework,anonymous-types,C#,Linq,Entity Framework,Anonymous Types,我使用C4ASP.net和EF4。我正在预编译一个查询,结果应该是匿名类型的集合 目前我使用这个代码 public static readonly Func<CmsConnectionStringEntityDataModel, string, dynamic> queryContentsList = CompiledQuery.Compile<CmsConnectionStringEntityDataModel, string, dynamic> (

我使用C4ASP.net和EF4。我正在预编译一个查询,结果应该是匿名类型的集合

目前我使用这个代码

public static readonly Func<CmsConnectionStringEntityDataModel, string, dynamic>     
queryContentsList =
CompiledQuery.Compile<CmsConnectionStringEntityDataModel, string, dynamic>
(
    (ctx, TypeContent) => ctx.CmsContents.Where(c => c.TypeContent == TypeContent 
 & c.IsPublished == true & c.IsDeleted == false)
        .Select(cnt => new
      { 
         cnt.Title, 
         cnt.TitleUrl, 
         cnt.ContentId, 
         cnt.TypeContent, cnt.Summary 
      }
            )
   .OrderByDescending(c => c.ContentId));

您不应该从方法返回匿名类型。创建一个具体类型来保存匿名类型中当前保存的任何数据,并返回该数据

...
.Select(cnt => 
    new ConcType{ 
        Title = cnt.Title, 
        TitleUrl = cnt.TitleUrl, 
        ContentId = cnt.ContentId, 
        TypeContent = cnt.TypeContent,  
        Summary = cnt.Summary })
...
其中:

class ConcType
{
    public string Title {get; set;}
    //etc...
}

您不应该从方法返回匿名类型。创建一个具体类型来保存匿名类型中当前保存的任何数据,并返回该数据

...
.Select(cnt => 
    new ConcType{ 
        Title = cnt.Title, 
        TitleUrl = cnt.TitleUrl, 
        ContentId = cnt.ContentId, 
        TypeContent = cnt.TypeContent,  
        Summary = cnt.Summary })
...
其中:

class ConcType
{
    public string Title {get; set;}
    //etc...
}

我得到这个错误。。。。。ConcType它不实现“System.Collections.IEnumerable”如果你在问题中添加更新的代码,可能会更容易告诉你发生了什么。谢谢spender,我更新了代码,如果你有时间请告诉我。呃,你可以从方法返回匿名类型,但不能返回强类型。如果方法签名要返回对象或此处为动态,则返回新的{a=5};很好用。原来的错误和注释都表明序列和单个对象之间存在混淆。我理解你的观点。你有务实的方法来解决这个问题吗?谢谢,我得到了这个错误。。。。。ConcType它不实现“System.Collections.IEnumerable”如果你在问题中添加更新的代码,可能会更容易告诉你发生了什么。谢谢spender,我更新了代码,如果你有时间请告诉我。呃,你可以从方法返回匿名类型,但不能返回强类型。如果方法签名要返回对象或此处为动态,则返回新的{a=5};很好用。原来的错误和注释都表明序列和单个对象之间存在混淆。我理解你的观点。你有务实的方法来解决这个问题吗?谢谢