How to solve generic predicate in where clause item?
i face to face 3 errors on where clause" _list.Where
Error 1
'System.Collections.Generic.List
Error 2 Instance argument: cannot convert from 'System.Collections.Generic.List
Error 3 Argument 2: cannot convert from 'System.Func
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FuncMetodunuTaniyalim3
{
public class Search<T> where T : class
{
private List<DataModel.Customer> _list;
public Search()
{
_list = new List<DataModel.Customer>();
}
public int Find(Func<T, bool> whereClause)
{
return _list.IndexOf(_list.Where<T>(whereClause).FirstOrDefault<T>());
}
}
//Repository
public class DataModel
{
private IEnumerable<Customer> customers;
public List<Customer> Customers
{
get { return customers.ToList(); }
}
public DataModel()
{
customers = new[] { new Customer(){ Id=1, Name="cbfghg", SurName="hfh", Age=29, Dept=0, Income=100},
new Customer(){ Id=2, Name="hfghfhf", SurName="erhfghfhfhem", Age=0,Dept=45, Income=300},
new Customer(){ Id=3, Name="hgfhgfhf", SurName="balhfghgfhfgh", Age=33, Dept=20, Income=150}};
}
//Model
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string SurName { get; set; }
public int Age { get; set; }
public int Income { get; set; }
public int Dept { get; set; }
public Customer()
{
}
}
}
}
Your Search class contains a List<Customer>
. Probably you meant to use List<T>
:
public class Search<T> where T : class
{
private List<T> _list;
public Search()
{
_list = new List<T>();
}
public int Find(Func<T, bool> whereClause)
{
return _list.IndexOf(_list.Where<T>(whereClause).FirstOrDefault<T>());
}
}
Your list is a List<DataModel.Customer>
, while your predicate is a Func<T, bool>
. Change the type of your predicate to Func<DataModel.Customer, bool>
or change your list type to List<T>
.
你不能这样做,因为List<DataModel.Customer>
实现IEnumerable<DataModel.Customer>
请看方法文档:http://msdn.microsoft.com/en-us/library/bb534803.aspx也许你想要_list作为List<T>
在那里?
上一篇: 根据下拉列表中的值自动完成显示数据
下一篇: 如何解决where子句项中的泛型谓词?