Solved

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

Posted on 2012-04-12
2
369 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
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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