1

I have a function that gets a filtered list of items based on the lambda expression I pass in. Below is an example of what I am doing. The List is an ObservableCollection of myBase and the filter I am passing in would be something like this: t => t.Data.Any()

At the moment if I replace "filter" with the above lambda it works but when I pass it in and use the local variable, filter, I get a compile error like “cannot be inferred from the usage. Try specifying the type arguments explicitly.”</p>

    protected IEnumerable<myBase> GetByFilter(Expression<Func<myBase, bool>> filter)
    {
        IEnumerable<myBase> itemlList = _items.Where(filter).ToList();
        return itemlList ;
    }

What am I missing here?

Edit -------------------

I am trying to get a subset of the original list based on the lambda passed in. I think I may be able to get away with the lambda line returning another observableCollection rather than an IEnumerable one, if that is possible?

Edit -------------------

With help from Ruslan, I have fixed my problem. My code now compiles and looks like this:

protected IEnumerable<myBase> GetByFilter(Func<myBase, bool> filter) 
{ 
    IEnumerable<myBase> itemlList = _items.Where(filter).ToList(); 
    return itemlList ; 
} 

I can pass in a filter like "t => t.Data.Any()" and get all the items etc. I just needed to drop "Expression" from the filter parameter.

4

2 回答 2

2

我不明白这个示例如何在将 Queryable 扩展“Where”应用于 Enumerable 集合的情况下进行编译。而且它的使用方式仍然不完全清楚。但是,以下内容会编译并运行。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace TestProject
{
    class myBase
    {
        public int Id;
        public string Data;
    }

    class Program
    {
        static ObservableCollection<myBase> _items;

        static IEnumerable<myBase> GetByFilter(Func<myBase, bool> filter)
        {
            IEnumerable<myBase> itemlList = _items.Where(filter).ToList();
            return itemlList;
        }

        static void Main(string[] args)
        {
            _items = new ObservableCollection<myBase> { 
                new myBase { Id = 1, Data = "" },
                new myBase { Id = 2, Data = "Data" },
                new myBase { Id = 3, Data = "More Data" }
            };

            IEnumerable<myBase> filteredList = GetByFilter(t => t.Data.Any());

            foreach (var item in filteredList)
                Console.WriteLine("{0}: {1}", item.Id, item.Data);
        }
    }
}

结果:

2: Data
3: More Data
于 2012-06-28T18:35:54.393 回答
0

问题似乎是您的过滤器是 type Expression<Func<IdcBase, bool>>,但是(根据评论)该集合的Where方法需要一个. _itemsFunc<myBase, bool>

我不知道MyBaseand之间的关系是什么,IdcBase但我会假设IdcBase继承自MyBase.

如果上述假设是正确的,那么您将无法过滤MyBase使用预期为 的过滤器的列表IdcBase,因为您可能会有_items不属于 类型的条目IdcBase。您需要提供一个需要 type 输入的过滤器MyBase,或者您需要首先限制_items那些 type IdcBase

protected IEnumerable<myBase> GetByFilter(Expression<Func<IdcBase, bool>> filter)
{
    IEnumerable<myBase> itemlList = _items.OfType<IdcBase>().Where(filter).ToList();
    return itemlList ;
}
于 2012-06-28T15:26:41.227 回答