简体   繁体   中英

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!

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();

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> .

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM