문제

내가 붙어를 만들려고 동적 linq extension 방법은 문자열을 반환하는 JSON 형식으로-나가 사용하는 시스템입니다.Linq.동적 및 Newtonsoft.Json 세계적인 신뢰를 얻고 있는 최고 읽습니다.동적을 분석하는"세포=new object[]"부분입니다.아마도 너무 복잡?어떤 아이디어가?:

나의 주요한 방법:

static void Main(string[] args)
{
    NorthwindDataContext db = new NorthwindDataContext();
    var query = db.Customers;
    string json = JSonify<Customer>
                    .GetJsonTable(
                        query, 
                        2, 
                        10, 
                        "CustomerID"
                        , 
                        new string[] 
                            { 
                                "CustomerID", 
                                "CompanyName", 
                                "City", 
                                "Country", 
                                "Orders.Count"
                            });
    Console.WriteLine(json);
}

JSonify 클래스

public static class JSonify<T>
{
    public static string GetJsonTable(
        this IQueryable<T> query, 
        int pageNumber, 
        int pageSize, 
        string IDColumnName, 
        string[] columnNames)
    {
        string selectItems =
            String.Format(@"
                        new
                        {
                            {{0}} as ID,
                            cell = new object[]{{{1}}}
                        }", 
                          IDColumnName, 
                          String.Join(",", columnNames));

        var items = new
        {
            page = pageNumber,
            total = query.Count(),
            rows =
                query
                    .Select(selectItems)
                    .Skip(pageNumber * pageSize)
                    .Take(pageSize)
        };

        return JavaScriptConvert.SerializeObject(items);
        // Should produce this result:
        // {
        //    "page":2,
        //    "total":91,
        //    "rows":
        //        [
        //        {"ID":"FAMIA","cell":["FAMIA","Familia Arquibaldo","Sao Paulo","Brazil",7]},
        //        {"ID":"FISSA","cell":["FISSA","FISSA Fabrica Inter. Salchichas S.A.","Madrid","Spain",0]},
        //        {"ID":"FOLIG","cell":["FOLIG","Folies gourmandes","Lille","France",5]},
        //        {"ID":"FOLKO","cell":["FOLKO","Folk och fä HB","Bräcke","Sweden",19]},
        //        {"ID":"FRANK","cell":["FRANK","Frankenversand","München","Germany",15]},
        //        {"ID":"FRANR","cell":["FRANR","France restauration","Nantes","France",3]},
        //        {"ID":"FRANS","cell":["FRANS","Franchi S.p.A.","Torino","Italy",6]},
        //        {"ID":"FURIB","cell":["FURIB","Furia Bacalhau e Frutos do Mar","Lisboa","Portugal",8]},
        //        {"ID":"GALED","cell":["GALED","Galería del gastrónomo","Barcelona","Spain",5]},
        //        {"ID":"GODOS","cell":["GODOS","Godos Cocina Típica","Sevilla","Spain",10]}
        //        ]
        // }

    }

}
도움이 되었습니까?

해결책

이것은 정말 못생긴 몇 가지 문제가 있을 수 있습과 문자열을 보충,그러나 그것은 예상한 결과:

public static class JSonify
{
    public static string GetJsonTable<T>(
        this IQueryable<T> query, int pageNumber, int pageSize, string IDColumnName, string[] columnNames)
    {
        string select = string.Format("new ({0} as ID, \"CELLSTART\" as CELLSTART, {1}, \"CELLEND\" as CELLEND)", IDColumnName, string.Join(",", columnNames));
        var items = new
        {
            page = pageNumber,
            total = query.Count(),
            rows = query.Select(select).Skip((pageNumber - 1) * pageSize).Take(pageSize)
        };
        string json = JavaScriptConvert.SerializeObject(items);
        json = json.Replace("\"CELLSTART\":\"CELLSTART\",", "\"cell\":[");
        json = json.Replace(",\"CELLEND\":\"CELLEND\"", "]");
        foreach (string column in columnNames)
        {
            json = json.Replace("\"" + column + "\":", "");
        }
        return json;
    }
}  

다른 팁

static void Main(string[] args)
{
    NorthwindDataContext db = new NorthwindDataContext();
    var query = db.Customers;
    string json = query.GetJsonTable<Customer>(2, 10, "CustomerID", new string[] {"CustomerID", "CompanyName", "City", "Country", "Orders.Count" });
 }  

public static class JSonify
{
    public static string GetJsonTable<T>(
        this IQueryable<T> query, int pageNumber, int pageSize, string IDColumnName, string[] columnNames)
    {
        string select = string.Format("new ({0} as ID, new ({1}) as cell)", IDColumnName, string.Join(",",     columnNames));
        var items = new
        {
            page = pageNumber,
            total = query.Count(),
            rows = query.Select(select).Skip((pageNumber - 1) * pageSize).Take(pageSize)
        };
        return JavaScriptConvert.SerializeObject(items);
    }
}  

감사에 대한 빠른 응답을 할 수 있습니다.그러나 참고 필요한 출력이 없 속성 이름에"셀룰라"배열로(그 이유는 내가 사용하는 개체는[]):

"셀":["FAMIA","나 Arquibaldo",...대"셀":{"CustomerID":"FAMIA","회사","나 Arquibaldo",...

그 결과로 사용하는 것을 의미한 JQuery 격자"라는 flexify"는 필요한 출력에서는 이 형식입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top