2009-04-15 16 views
3
中と拡張メソッド

私の拡張メソッドは、次のとおりです。LINQツーSQLサブクエリ

public static IEnumerable<T> FilterCultureSubQuery<T>(this Table<T> t) 
    where T : class 
    { 
     return t; 
    } 

私は私のメソッドのシグネチャことになって何このクエリ

var test = from p in t.Products 
      select new 
      { 
       Allo = p, 
       Allo2 = (from pl in t.ProductLocales.FilterCultureSubQuery() 
         select pl) 
      }; 

でそれを使用しようとしました拡張?私はいつもこのエラーが出る:

 
Method 'System.Collections.Generic.IEnumerable`1[ProductLocale] FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])' has no supported translation to SQL. 

私もこの署名しようとした:

public static IQueryable<T> FilterCultureSubQuery<T>(this Table<T> t) 
    where T : class 
    { 
     return t; 
    } 

をそして、私はこのエラーを得た:あなたのメソッドのシグネチャがある

 
Method 'System.Linq.IQueryable`1[ProductLocale] FilterCultureSubQuery[ProductLocale](System.Data.Linq.Table`1[ProductLocale])' has no supported translation to SQL. 

おかげ

答えて

1

単純なクエリで拡張メソッドを使用すると機能しますが、サブクエリで使用すると機能しません。どんな解決策ですか?私は新しい拡張メソッドを作成し、クエリの式ツリーを書き換え

var test = from p in t.Products 
      select new 
      { 
      Allo = p, 
      Allo2 = (from pl in t.ProductLocales.FilterCultureSubQuery() 
         select pl) 
      }; 

を動作しない

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) 
       }).ArrangeExpression(); 

LINQ-TO-SQLはサブクエリで拡張メソッドを使用することができません。表現の拡張を書き直す方法がうまく動作します。

他の解決策はありますか?

4

を良い。問題は、前述のように、「SQLへの変換はサポートされていません」ということです。

DLINQは、そのステートメントをデータベースに送信するSQL行に変換しようとしています。その方法には翻訳がありません。

Where句を使用してフィルタを書き直すことをお勧めします。

2

Extensionメソッドに問題はありません。

LINQ-To-SQLクエリでカスタムメソッドを使用しようとしていて、LINQ-To-SQLがメソッドのSQLへの変換を知らないため、この例外が発生します。したがって、LINQ式からSQLクエリを作成することはできません。

解決策は、まずデータを取得してから変換を適用することです。