2

convert.i が int.Parse() 、 SqlFunction 、 EdmFunction を試したときに LINQ to Entities Int32 ToInt32(System.String) を取得しましたが、問題はまだ続いています。

例外:

System.NotSupportedException: LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

コード:

try
    {
        ModelEntities me = new ModelEntities();
        var query = from p in me.Products
                    join c in me.ProductCategories
                    on Convert.ToInt32(p.CategoryId) equals c.CategoryId
                    select new
                    {
                        p.ProductTitle,
                        c.CategoryName
                    };
        rptProducts.DataSource = query;
        rptProducts.DataBind();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
4

2 に答える 2

9

linq クエリ内で Convert.ToInt32 を使用することはできません。Linq には独自の構文があり、外部メソッドを認識しません。

探している変数を C# に抽出して変換し、別のクエリで変数として使用する必要があります。または、データベースにアクセスできる場合は、両方のカテゴリ ID を int にすることもできます。これらのような類似のフィールドが同じタイプであることは理にかなっています。

それが役立つことを願っています!

于 2012-05-24T13:48:42.887 に答える
4

このように c.CategoryId を文字列に変換することをお勧めします

 var query = from p in me.Products
             from c in me.ProductCategories
             let categoryId = me.ProductCategories.Take(1).Select(x => c.CategoryId).Cast<string>().FirstOrDefault()
             where p.CategoryId == categoryId 
             select new
             {
               p.ProductTitle,
               c.CategoryName
             };
于 2012-05-24T13:55:31.143 に答える