2017-08-21 17 views
-1

私は、次のRootObjectオブジェクトを用意する必要があります。オブジェクトのリストからオブジェクトを作成する

public class Document2 
{ 
    public string homecommunityid { get; set; } 
    public string repositoryid { get; set; } 
    public string documentuuid { get; set; } 
    public string doctype { get; set; } 
} 
public class Document 
{ 
    public Document2 document { get; set; } 
}  
public class RootObject 
{ 
    public List<Document> documents { get; set; } 
} 

次のDocumentQueryオブジェクトのリストからRootObjectオブジェクトを取り込む必要があります。

public class DocumentQuery 
{ 
     public string doctype { get; set; } 
     public string homecommunityid { get; set; } 
     public string repositoryid { get; set; } 
     public string documentuuid { get; set; } 
} 

私は次のように何かをしようとしています:

RootObject doc = new RootObject() 
    { 
     Document= obj.Select(s => new Document() 
     { 
      document = new Document2() 
      { 
       doctype = s.doctype 
       } 
     }) 
    }; 

私はこの形式でできJSONになるこの必要があります。

{ 
    "documents": [ 
    { 
     "document": { 
     "homecommunityid": "", 
     "repositoryid": "", 
     "documentuuid": "", 
     "doctype": "" 
     } 
    } 
    ] 
} 
+0

これまでに試したコードを共有してください...コミュニティがあなたにリファクタリングして助けます.. –

+2

どうして2つのクラス( 'Document2'と' DocumentQuery')が本質的に同じですか?なぜあなたは何も役に立たないように見える介入型( 'Document')を持っていますか?あなたは 'RootObject'をまだ移植しようとしましたか?それはLINQでかなりシンプルにすべきです。 –

+0

基本に戻る。それは単に新しいクラスの問題です。 – niksofteng

答えて

1

場合は、単に、次のコードを使用することができますあなたはリフレクションなどで遊びたくありません。

0

AutoMapperを使用して解決できます。

AutoMapperは、1つのオブジェクトを別のオブジェクトにマップしたコードを削除するという、致命的な複雑な問題を解決するために構築された簡単な小さなライブラリです。

関連する問題