Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 500247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:04:04+00:00 2026-05-13T06:04:04+00:00

I have some linq to sql method and when it does the query it

  • 0

I have some linq to sql method and when it does the query it returns some anonymous type.

I want to return that anonymous type back to my service layer to do some logic and stuff on it.

I don’t know how to return it though.

I thought I could do this

public List<T> thisIsAtest()
{
     return query;
}

but I get this error

Error   1   The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

So not sure what assembly I am missing or if that is even the case.

Thanks

EDIT

Ok my first problem was solved but now I have a new problem that I am not sure how to fix since I don’t know much about anonymous types.

I get this error

Cannot implicitly convert type
‘System.Collections.Generic.List’
to ‘System.Collections.Generic.List

Here is the query

   DbContext.Table.Where(u => u.Table.UserId == userId && u.OutOFF != 0)
       .GroupBy(u => new { u.Table.Prefix })
       .Select(group => new { prefix = group.Key, 
                              Marks = group.Sum(item => (item.Mark * item.Weight) / item.OutOFF) })
       .ToList();

Edit 2

public class ReturnValue
{
   string prefix { get; set; }
   decimal? Marks { get; set; } 
}

public List<ReturnValue> MyTest(Guid userId)
{
   try
   {
       var result = dbContext.Table.Where(u => u.Table.UserId == userId && u.OutOFF != 0).GroupBy(u => new { u.Table.Prefix })
       .Select(group => new { prefix = group.Key, Marks = group.Sum(item => (item.Mark * item.Weight) / item.OutOFF) }).ToList();
       return result;
   }
   catch (SqlException)
   {
       throw;
   }

the select has this in it

Anonymous Types:

a is new{string Prefix}
b is new{ 'a prefix, decimal? marks}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:04:04+00:00Added an answer on May 13, 2026 at 6:04 am

    You can’t – period. You cannot use anonymous types outside their own scope, e.g. you cannot return them as return values from a method.

    If you need to return them, you need to define a new concrete class instead of the anonymous type, and use that in the place of the anonymous type.

    See Rick Strahl’s blog post on the scoping of anonymous types, and see the MSDN docs here which clearly state:

    An anonymous type has method scope. To
    pass an anonymous type, or a
    collection that contains anonymous
    types, outside a method boundary, you
    must first cast the type to object.

    However, this defeats the strong
    typing of the anonymous type. If you
    must store your query results or pass
    them outside the method boundary,
    consider using an ordinary named
    struct or class instead of an
    anonymous type.

    OK, sure – there are dirty awful hacks to indeed return anonymous types. But if Microsoft’s MSDN AND Jon Skeet discourage that practice, then – just don’t do it. By definition and intention, anonymous types are bound to the method they’re defined in.

    UPDATE for chobo2: I don’t know what your datatypes are – just guessing – but assuming “prefix” is an int and “marks” is a decimal, you could define a new class:

    public class ReturnValue
    {
        int prefix { get; set; }
        decimal Marks { get; set; } 
    }  
    

    and then your code would be a method that returns a List<ReturnValue>:

    public List<ReturnValue> thisIsAtest()
    {
       DbContext.Table.Where(u => u.Table.UserId == userId && u.OutOFF != 0)
         .GroupBy(u => new { u.Table.Prefix })
         .Select(group => new ReturnValue 
                              { prefix = group.Key, 
                                Marks = group
                                  .Sum(item => (item.Mark * item.Weight) / item.OutOFF) })
         .ToList();
    }
    

    The key here is: in your .Select method, instead of creating a new instance of an anonymous type:

         .Select(group => new { prefix = group.Key, marks = .... }
    

    you create an instance of a concrete type:

         .Select(group => new ReturnValue { prefix = group.Key, marks = .... }
    

    This way, you’ll have a concrete class ReturnValue – name that anything you like – and then you can easily return a list of that type, and use that type elsewhere, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 273k
  • Answers 273k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer At least 1 problem is the missing comma here: maxlengh:… May 13, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer You might be able to tell by running a trace… May 13, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer Your query will need to count off the first 90M… May 13, 2026 at 2:05 pm

Related Questions

I have created a universalrepository that takes the type passed to it and when
I created one asp.net mvc application using linq to sql, and in the generated
I have a Linq-to-SQL class, and I'd like to perform some pre-save validation before
I do experiment with LINQ since some time. Typical method to enumerate through a
Suppose I have an IQueryable<T> expression that I'd like to encapsulate the definition of,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.