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 793015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:05:02+00:00 2026-05-14T22:05:02+00:00

If I have an anonymous type created by LINQ var ans = from r

  • 0

If I have an anonymous type created by LINQ

var ans = from r in someList where someCondition(r) select new { r.a, r.b };

What is the best way to create an empty matching collection so I can move elements to the new collection:

var newans = ?
foreach (r in ans) { if (complicated(r)) newans.Add(r); }

Is there some way to use Enumerable.Empty<>()?

  • 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-14T22:05:03+00:00Added an answer on May 14, 2026 at 10:05 pm

    I wouldn’t use the ans.Where(x => false).ToList() versions these iterate over all elements, which you don’t need; ans.Take(0).ToList() is a little better, as this one doesn’t actually iterate over the entire list when you’re querying over in-memory sequences (LINQ to Objects).

    If you are using LINQ to SomethingElse, things are a little problematic, because it might actually execute something like SELECT TOP(0) * FROM ... or SELECT * FROM ... WHERE 1 = 0. Not good.

    So the following helper method will help you:

    public static class AnonymousTypeExtensions
    {
        public static List<T> ToEmptyList<T>(this IEnumerable<T> source)
        {
            return new List<T>();
        }
    }
    
    var newans = ans.ToEmptyList();
    

    Having said that, if your only desire is to copy some elements of ans into a new list, you’re better of with

    var newans = (from a in ans where isComplicated(a) select a).ToList();
    

    or

    var newans = ans.Where(a => isComplicated(a)).ToList();
    

    If you don’t need an actual List<T>, and an IEnumerable<T> is enough, you might get away with omitting the ToList altogether. Mind you, every time you foreach over such an enumerable, Where(a => isComplicated(a)) is executed again and again. The call to ToList makes sure you only execute it once.


    If you don’t want this just for List<T>, but more general, things get a lot more complicated. For example, should the method return the static type of the ans variable or its runtime type? Also, there is no such thing as a “default” version of many collections, apart from blindly calling new C(). But that won’t work generally. It’s not possible to call new ReadOnlyCollection<T>(), because it doesn’t exist (and the collection would be sealed anyway, so it cannot be filled). And if you’re using a HasSet, shouldn’t you be using the original’s comparer for your empty copy rather than the default comparer?

    However, for the simplest case, you could try:

    public static class CollectionExtensions
    {
        public static TCollection AsEmpty<TCollection>(this TCollection source)
            where TCollection : ICollection, new()
        {
            return new TCollection();
        }
    }
    

    Note that this is a compile-time solution which returns a default collection of the variable‘s type. If at all possible. For instance ans = from r in somesource select new { ... } will have the static type IEnumerable<...>. There’s no default instance here to create. Also, the runtime type returned by select is private to System.Core.dll, doesn’t have a default constructor, and is a read-only cursor-like type anyway.

    So my opinion on this one is: don’t go there. Don’t try to over-generalize, stick to simple solutions like ToEmptyList or even better, stick to plain LINQ method chaining.

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

Sidebar

Related Questions

I have a LINQ statement that returns an anonymous type. I need to get
Into some view data i have put the result of an anonymous type: var
Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to
I have a DropDownList populated with a SelectList generated from (an anonymous type) that
I have a Linq query that returns an anonymous type, which I then use
I have two collections, and need to create a new collection from the two
Is it possible to have an anonymous type implement an interface? I've got a
I have no clue what an anonymous type is in C# nor how it
I have a question of how to sort an anonymous type. Using Linq2SQL I
I have an anonymous linq query that I bind to a datagrid, when I

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.