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

  • SEARCH
  • Home
  • 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 609661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:35:25+00:00 2026-05-13T17:35:25+00:00

I am just getting my feet wet with LINQ to SQL, and need some

  • 0

I am just getting my feet wet with LINQ to SQL, and need some advice in implementing a simple scenario:

I have a method ListTagCounts that is doing an aggregate query with LINQ To SQL. I was not sure of the return type to use, so used the ToList() method and created a class just for the return type in order to get it to compile.

Schema:

Link         LinkTag      Tag
----------   ----------   ----------
LinkID       LinkID       TagID
URL          TagID        Name
IsDeleted

Code:

static void Main(string[] args)
{
    var counts = ListTagCounts();
    foreach(var c in counts)
        Console.WriteLine("Tag: {0}, Count: {1}", c.Tag, c.Count);
    Console.ReadKey();
}

public static List<MyReturnClass> ListTagCounts()
{
    List<MyReturnClass> counts;
    using (var db = new MyDbDataContext())
    {
        counts = (from t in db.Tags
                  join lt in db.LinkTags on t.TagID equals lt.TagID
                  join l in db.Links on lt.LinkID equals l.LinkID
                  where l.IsDeleted == false
                  select new MyReturnClass() { Tag = t.Name, Count = t.LinkTags.Count() }
        ).Distinct().OrderBy(t => t.Tag).ToList();
    }
    return counts;
}

public class MyReturnClass
{
    public string Tag { get; set; }
    public int Count { get; set; }
}

If I do

select new { Tag = t.Name, Count = t.LinkTags.Count() }

rather than

select new MyReturnClass() { Tag = t.Name, Count = t.LinkTags.Count() }

in my LINQ query, what should the return type be?

  • 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-13T17:35:25+00:00Added an answer on May 13, 2026 at 5:35 pm

    When you use the following syntax:

    select new { Tag = ..., Count = ... };
    

    You are creating an anonymous type. The compiler generates the name of this type for you at compile-time, so there is no way for you to know what it is.

    When you call ToList on the query that selects instances of this anonymous type, it’s possible that you can return the list because List implements some non-generic classes (which obviously don’t rely on the type parameter T, which you aren’t aware of) you can cast to.

    Generally though, you should not let anonymous types escape the method that they are used in. If you have the need to pass the information outside of the method that the query is in, then what you are doing is correct, in creating a specialized class and then returning a sequence of that.

    Note there are some justified uses for passing anonymous types outside of your method (through a return type of object, or other base classes for sequences of these types), but for cases like this, it isn’t one of them.

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

Sidebar

Related Questions

just getting my feet wet with sql server express. I have what would be
Just getting my feet wet with some Fluent NHibernate AutoMap conventions, and ran into
I'm just getting my feet wet with Android and have built a UI that
Just getting the hang of Spring Webflow. I have some simple forms working and
I'm just getting my feet wet with Linq and IEnumerable, and I'm needing help
I'm just getting my feet wet with LINQ. Given three lists of items, this
I'm just getting my feet wet with Ember.js, and I've hit something that I'm
I'm pretty familiar with SQL syntax (via MySQL) and am just getting my feet
I'm just getting my feet wet in Silverlight, and don't really understand the differences
Disclaimer: I'm new to unit testing and just getting my feet wet. That being

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.