C# LINQ到SQL及子查询中的扩展方法
我的扩展方法是:C# LINQ到SQL及子查询中的扩展方法,c#,linq,linq-to-sql,extension-methods,C#,Linq,Linq To Sql,Extension Methods,我的扩展方法是: public static IEnumerable<T> FilterCultureSubQuery<T>(this Table<T> t) where T : class { return t; } 我的方法扩展的签名应该是什么?我总是会遇到这样的错误: Method 'System.Collections.Generic.IEnumerable`1[ProductLocale] FilterCultu
public static IEnumerable<T> FilterCultureSubQuery<T>(this Table<T> t)
where T : class
{
return t;
}
我的方法扩展的签名应该是什么?我总是会遇到这样的错误:
Method 'System.Collections.Generic.IEnumerable`1[ProductLocale] FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])' has no supported translation to SQL.
Method 'System.Linq.IQueryable`1[ProductLocale] FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])' has no supported translation to SQL.
方法“System.Collections.Generic.IEnumerable`1[ProductLocale]FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])不支持到SQL的转换。
我也试过这个签名:
public static IQueryable<T> FilterCultureSubQuery<T>(this Table<T> t)
where T : class
{
return t;
}
公共静态IQueryable FilterCultureSubQuery(此表为t)
T:在哪里上课
{
返回t;
}
我得到了这个错误:
Method 'System.Collections.Generic.IEnumerable`1[ProductLocale] FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])' has no supported translation to SQL.
Method 'System.Linq.IQueryable`1[ProductLocale] FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])' has no supported translation to SQL.
方法'System.Linq.IQueryable`1[ProductLocale]FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])不支持到SQL的转换。
谢谢您的方法的签名很好。问题是,如前所述,它“没有支持的SQL转换” DLINQ正在尝试将该语句转换为一行SQL,并将其发送到数据库。这种方法没有翻译
我建议使用Where子句重写筛选器。扩展方法没有问题 之所以会出现这种异常,是因为您试图在LINQ to SQL查询中使用自定义方法,而LINQ to SQL不知道您的方法是否转换为SQL。因此,它无法从LINQ表达式构造SQL查询
解决方案是首先获取数据,然后应用转换。当我在简单查询中使用扩展方法时,它是有效的,但当我在子查询中使用它时,它不起作用。有什么解决办法吗 工作
var test = from pl in t.ProductLocales.FilterCultureSubQuery() select pl;
不起作用
var test = from p in t.Products
select new
{
Allo = p,
Allo2 = (from pl in t.ProductLocales.FilterCultureSubQuery()
select pl)
};
我创建了一个新的扩展方法并重写了查询的表达式树
var test = (from p in t.Products
select new
{
Allo = p,
Allo2 = (from pl in t.ProductLocales.FilterCultureSubQuery()
select pl)
}).ArrangeExpression();
LINQ-TO-SQL很难在子查询中使用扩展方法。使用重写表达式扩展方法,一切正常
还有其他解决办法吗