0

LINQ to Entities はメソッド 'System.String ToString()' メソッドを認識せず、このメソッドはストア式に変換できません。

    public ActionResult PopulateFromDB(string sidx, string sord, int page, int rows)
    {
        var context = new NerdDinnerEntities();
        var jsonData = new
        {
            total = 1,
            page = page,
            sord =sord,
            records = context.Authors.Count(),
            rows = (from n in context.Authors
                    select new
                    { AuthorId = n.AuthorId ,
                        cell = new string[] { n.AuthorId.ToString(), n.Name.ToString(), n.Location.ToString() }
                    }).ToList()
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }

ToList または Toarray を書いていますが、エラーが発生します:

    public ActionResult PopulateFromDB(string sidx, string sord, int page, int rows)
    {
        var context = new NerdDinnerEntities();
        var jsonData = new
        {
            total = 1,
            page = page,
            sord =sord,
            records = context.Authors.Count(),
            rows = (from n in context.Authors
                    select new
                    { AuthorId = n.AuthorId ,
                        cell = new string[] { n.AuthorId.ToString(), n.Name.ToString(), n.Location.ToString() }
                    }).ToList()
        };
        return Json(jsonData,JsonRequestBehavior.AllowGet); 
    }
4

2 に答える 2

0

あなたのコードからcell、クライアント側で表示/保存の目的でカスタム プロパティを追加すると仮定します。API呼び出しを1つの特定のクライアントに本質的に結合するため、これは避けます。必要なデータを返すだけで、具体的にはクライアント側で処理することをお勧めします。

サーバ

...
select new
{
    Id = n.AuthorId,
    Name = n.Name,
    Location = n.Location
}).ToList();
...

クライアント

var response = ...
foreach (var author in response)
{
    var cell = new string[] { author.Id.ToString(), author.Name, author.Location };
    // do something with cell
}
于 2012-10-29T12:48:34.827 に答える
0

これを変換するには、 SqlFunctions.StringConvertを試す必要があります。int のオーバーロードがないため、数値を double または decimal にキャストする必要があります。

public ActionResult PopulateFromDB(string sidx, string sord, int page, int rows)
{
    var context = new NerdDinnerEntities();
    var jsonData = new
    {
        total = 1,
        page = page,
        sord =sord,
        records = context.Authors.Count(),
        rows = (from n in context.Authors
                select new
                { AuthorId = n.AuthorId ,
                  cell = new string[] { SqlFunctions.StringConvert((double)n.AuthorId), n.Name, n.Location }
                }).ToList()
    };
    return Json(jsonData,JsonRequestBehavior.AllowGet); 
}

コードが機能するはずのLinqToSqlクラスを使用していませんが、LinqToEntityを使用していると述べたようSqlFunctions.StringConvertに、文字列に変換するために使用する必要があります。

于 2012-10-29T12:40:39.917 に答える