Linq - How to collect Anonymous Type as Result for a Function

Posted by GibboK on Stack Overflow See other posts from Stack Overflow or by GibboK
Published on 2011-11-29T09:12:51Z Indexed on 2011/11/29 9:50 UTC
Read the original article Hit count: 245

I use c# 4 asp.net and EF 4. I'm precompiling a query, the result should be a collection of Anonymous Type.

At the moment I use this code.

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

I suspect the RETURN for the FUNCTION Dynamic does not work properly and I get this error

Sequence contains more than one element enter code here.

I suppose I need to return for my function a Collection of Anonymous Types...

Do you have any idea how to do it? What I'm doing wrong? Please post a sample of code thanks!

Update:

    public class ConcTypeContents
        {
            public string Title { get; set; }
            public string TitleUrl { get; set; }
            public int ContentId { get; set; }
            public string TypeContent { get; set; }
            public string Summary { get; set; }
        }

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ