我的扩展:
public class IQueryableExtensions
{
public static IQueryable<T> Filter<T>(this IQueryable<T> collection, Expression<Func<T, bool>> where, string value, out bool hasFilter)
{
hasFilter = false;
if (!String.IsNullOrEmpty(value))
{
collection = collection.Where(where);
hasFilter = true;
}
return collection;
}
}
我的代码:
IQueryable<GlobalLocation> customers = db.GlobalLocations;
customers.Filter(...); // Doesn't work
IQueryableExtensions.Filter<GlobalLocation>(customers, c => c.TBUID == searchTbuid, searchTbuid, out hasFilter); // Works
尝试通过customers
调用它甚至不会出现在intelligense中。为什么会这样,我可以/如何修复?
更改:
public class IQueryableExtensions
收件人:
public static class IQueryableExtensions
扩展方法应该是静态类中的静态方法。
您必须将扩展方法放在static
类中。
public static class IQueryableExtensions
{
// Place here your extension methods.
}
执行方法应该放在静态类中。
一方面不应该将静态类的名称从IQueryableExtensions
更改为QueryableExtensions
,这是类而不是接口。除了更清楚地表明QueryableExtensions
是一个类而不是接口之外,这不会有任何区别。
扩展类和方法签名必须是静态的。
使用
public static class IQueryableExtensions