Equilibrium23
Equilibrium23

Reputation: 65

CS1929 - List<T> does not contain an implementation of Where() (using System.Linq)

Have been struggling to clear this compiler error for some time now.

Compiler Error: Line 29 - 'List newCollection = tempCollection.Where(predicate);'

CS1929 'List' does not contain a definition for 'Where' and the best extension method overload 'ParallelEnumerable.Where(ParallelQuery, Func<T, bool>)' requires a receiver of type 'ParallelQuery'

As seen in the code snippet, I am attempting to call the extension method Where() from the Linq library with a predicate as the input arg. The collection I am trying to call this on is a List<T> - worth noting that I am trying to allow this new extension method SnapFingers to accept any IEnumerable of type T, I have a feeling this is where my issues are arising from. I understand being specific would probably fix this but I want to try to make it generic.

Here is the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace BootCamp.Chapter
{
    public static class SnapFingersLINQ
    {
        public static IEnumerable<T> SnapFingers<T>(this IEnumerable<T> items, Predicate<T> predicate = null)
        {
            if (!items.Any())
            {
                return default(List<T>);
            }
            else
            {
                if (predicate != null)
                {
                    
                    IEnumerable<T> shuffItems = LINQExtensions.Shuffle(items);
                    List<T> tempCollection = shuffItems.ToList();

                    List<T> newCollection = tempCollection.Where<T>(predicate);

                    int numToRemove = AmountToRemove(items);
                    newCollection.RemoveRange(0, numToRemove);
                    
                    return tempCollection;
                }
                else
                {
                    List<T> shuffItems = LINQExtensions.Shuffle(items).ToList();
                    int numToRemove = AmountToRemove(items);
                    shuffItems.RemoveRange(0, numToRemove);
                    return shuffItems;
                }
            }
        }

        private static int AmountToRemove<T>(IEnumerable<T> items)
        {
            int lengthCollection = items.Count();
            int numberOfElementsToRemove = lengthCollection / 2;
            if (lengthCollection % 2 == 0)
            {
                return numberOfElementsToRemove;
            }
            else
            {
                return numberOfElementsToRemove - 1;
            }
        }
    }
}

Note that LINQExtensions.Shuffle() just returns a new shuffled collection, IEnumerable<T> (I am aware Linq Extension 'Shuffle' exists but this was a challenge as part of a bootcamp homework).

Any help on this would be massively appreciated!

Upvotes: 1

Views: 4225

Answers (4)

Gellio Gao
Gellio Gao

Reputation: 843

Update: You should change the type of the second argument of the method SnapFingers<T> to Func<T, bool> predicate because the method Where<T> accepts that type rather than a Predicate<T>.

Original:

Try List<T> newCollection = tempCollection.Where<T>(predicate).ToList();

Explanation: the method Where of Linq returns IEnumerable<T>, you need to use ToList to turn it into a List<T>.

Upvotes: 1

Flydog57
Flydog57

Reputation: 7111

The IEnumerable<T>.Where method takes a Func<T, bool> not a Predicate<T>. Yes, they are essentially the same, but delegate types have distinct type identity, even if they are functionally the same.

Weirdly enough, I'm giving a Lunch & Learn session for.my extended team next week, and this is one of my examples

Upvotes: 1

Caleb Manning
Caleb Manning

Reputation: 11

I believe the problem here is that .Where expects an argument of type Func<T, bool>. Try List<T> newCollection = tempCollection.Where<T>(x => predicate.Invoke(x)).ToList();

Upvotes: 1

41686d6564
41686d6564

Reputation: 19641

Ther Where extension method expects a parameter of type Func<T, bool>, not Predicate<T>.

Change the signature of your method to:

public static IEnumerable<T> SnapFingers<T>(this IEnumerable<T> items, Func<T, bool> predicate = null)

..and then add a ToList() after the Where to avoid another compiler error:

List<T> newCollection = tempCollection.Where<T>(predicate).ToList();

Upvotes: 2

Related Questions