?
Solved

Define an Extension Method for IEnumerable<T> which returns IList<T>

Posted on 2012-04-12
2
Medium Priority
?
377 Views
Last Modified: 2012-04-16
Hi experts

Im trying to better understand linq and I was trying to have an Extension Method that returns a IList<T> instead of IEnumerable<T>

I have an Extension Method for IEnumerable<T> which returns IEnumerable<T>

static IEnumerable<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate)
{
    foreach(T value in data)
    {
        if(predicate(value)) yield return value;
    }
}

Open in new window


if I use this, I will get this error of "ïs not an iterator interface"

static IList<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate)
        {
            //Error	1	The body of 'ConsoleApplication1.EnumerableExtensions.Where<T>
            //(System.Collections.Generic.IEnumerable<T>, System.Func<T,bool>)' 
            //cannot be an iterator block because 'System.Collections.Generic.IList<T>' 
            //is not an iterator interface 

            foreach (T value in data)
            {
                if (predicate(value)) yield return value;
            }
        }

Open in new window


I think that the way to do it is like this

static IList<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate)
        {
            return Enumerable.Where(data, predicate).ToList();
        }

Open in new window


Is it valid for all cases? I will like to undertand it better

Thanks
0
Comment
Question by:JoseHidalgo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 37842560
You get that error because you are using the yield return construct, which mandates the method to return either IEnumerable or IEnumerable<T>.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37842668
I don't understand, as LINQ already provides the ToList extension method for IEnumerable.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question