2013-02-19 9 views
5

मैं एक अनाम प्रकार कैसे लौटा सकता हूं जो फ़ील्ड पैरामीटर पर निर्भर करता है जिसमें अज्ञात प्रकार में शामिल गुणों को सूचीबद्ध किया गया है? कार्य इकाई में 20 से अधिक गुण हैं, और ग्राहक गुणों के विभिन्न संयोजन प्राप्त करना चाहते हैं।कस्टम अनाम प्रकार कैसे लौटाते हैं?

public class Test 
{ 
    public class Task 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     //... more 20 properties 
    } 

    public List<Task> Tasks = new List<Task>(); 

    public Test() 
    { 
     Tasks.Add(new Task { Id = 1, Name = "Task #1", Description = "Description task #1" }); 
     Tasks.Add(new Task { Id = 2, Name = "Task #2", Description = "Description task #2" }); 
     Tasks.Add(new Task { Id = 3, Name = "Task #3", Description = "Description task #3" }); 
    } 

    public IEnumerable<object> GetAllTasks(string fields) 
    { 
     //if fields == 'Id,Name' then return anonymous type new { Id = t.Id, Name = t.Name } 
     return Tasks.Select(t => new { Id = t.Id, Name = t.Name }); 

     //if fields == 'Id,Name,Description' then return anonymous type new { Id = t.Id, name = t.Name, Description = t.Description } 
     return Tasks.Select(t => new { Id = t.Id, Name = t.Name, Description = t.Description }); 
    } 
} 
+3

बेनामी प्रकार संकलन प्रकार में जाना जाता है। आप रनटाइम पर गतिशील रूप से प्रकार बनाना चाहते हैं। यह काफी अलग है। इसका मतलब यह भी है कि 'GetAllTasks' के उपभोक्ता गतिशील प्रकारों से निपटने में सक्षम होना चाहिए। इसके अलावा, गुमनाम प्रकारों को वापस करने की अनुशंसा नहीं की जाती है। –

+0

आपने क्लास टास्क को परिभाषित किया है, आप अनाम प्रकार के बजाय इसका उपयोग क्यों नहीं करते? –

+0

आप कार्य ऑब्जेक्ट – glosrob

उत्तर

0

उस मामले में यदि कार्य IQueryable<Task> है और हम (उदाहरण के लिए एक डेटाबेस से) डेटा स्रोत से मजबूत करने के लिए की जरूरत है केवल उन स्तंभों जो ग्राहक के लिए आवश्यक हैं (DB सर्वर ओवरलोड नहीं), उपयोगी SelectDynamic विधि है। नतीजतन समाप्त निर्णय यहाँ ऐसे हो जाएगा:

public class Test 
{ 
    public class Task 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     //... more 20 properties 
    } 

    public IQueryable<Task> Tasks; 

    public Test() 
    { 
     Tasks = new List<Task> 
      { 
       new Task {Id = 1, Name = "Task #1", Description = "Description task #1"}, 
       new Task {Id = 2, Name = "Task #2", Description = "Description task #2"}, 
       new Task {Id = 3, Name = "Task #3", Description = "Description task #3"} 
      }.AsQueryable(); 
    } 

    public IEnumerable<object> GetAllTasks(string[] fields) 
    { 
     var response = new List<object>(); 
     var customTasks = Tasks.SelectDynamic(fields).Cast<dynamic>(); 
     foreach (var t in customTasks.Take(100)) 
     { 
      dynamic expando = new ExpandoObject(); 

      if (fields.Contains("Id")) expando.Id = t.Id; 
      if (fields.Contains("Name")) expando.Name = t.Name; 
      if (fields.Contains("Description")) expando.Description = t.Description; 
      // ... other properties 

      response.Add(expando); 
     } 
     return response; 
    } 
} 

एक नमूना उपयोग here

1

मुझे आशा है कि यह आप

public List<Task> Tasks = new List<Task>(); 

public void Test() 
{ 
    Tasks.Add(new Task { Id = 1, Name = "Task #1", Description = "Description task #1" }); 
    Tasks.Add(new Task { Id = 2, Name = "Task #2", Description = "Description task #2" }); 
    Tasks.Add(new Task { Id = 3, Name = "Task #3", Description = "Description task #3" }); 
} 

public ActionResult Index() 
{ 
    Test(); 

    return Json(GetAllTasks(), JsonRequestBehavior.AllowGet); 
} 

public IEnumerable<object> GetAllTasks() 
{ 
    return Tasks.Select(GetTask); 
} 

private object GetTask(Task task) 
{ 
    dynamic expandoObject = new ExpandoObject(); 
    //your if statment block 
    if (task.Id == 1) 
    { 
     expandoObject.Id = task.Id; 
    } 

    expandoObject.Name = task.Name; 
    expandoObject.Description = task.Description; 

    var dictionary = expandoObject as IDictionary<string, object>; 
    return dictionary.ToDictionary(item => item.Key, item => item.Value); 
} 

परिणाम देखें में मदद मिलेगी:

[ 
    [ 
     { 
     "Key":"Id", 
     "Value":1 
     }, 
     { 
     "Key":"Name", 
     "Value":"Task #1" 
     }, 
     { 
     "Key":"Description", 
     "Value":"Description task #1" 
     } 
    ], 
    [ 
     { 
     "Key":"Name", 
     "Value":"Task #2" 
     }, 
     { 
     "Key":"Description", 
     "Value":"Description task #2" 
     } 
    ], 
    [ 
     { 
     "Key":"Name", 
     "Value":"Task #3" 
     }, 
     { 
     "Key":"Description", 
     "Value":"Description task #3" 
     } 
    ] 
] 
+1

क्या आप यह बताने के लिए कुछ कथाएं जोड़ने पर विचार करेंगे कि यह कोड क्यों काम करता है, और यह सवाल का जवाब क्या बनाता है? प्रश्न पूछने वाले व्यक्ति के लिए यह बहुत उपयोगी होगा, और कोई और जो साथ आता है। –

1

आप ExpandoObject और System.Reflection उपयोग करने का प्रयास हो सकता है:

public IEnumerable<object> GetAllTasks(string[] fields) 
{ 
    List<object> response = new List<object>(); 
    Tasks.ForEach((a) => 
    { 
     dynamic expando = new ExpandoObject(); 
     var p = expando as IDictionary<String, object>; 
     foreach (string item in fields) 
     { 
      p[item] = a.GetType().GetProperty(item).GetValue(a); 
     } 
     response.Add(expando); 
    }); 
    return response; 
} 

और एक नमूना प्रयोग है:

static void Main(string[] args) 
{ 
    var test = new Test(); 
    var results = test.GetAllTasks(new[] { "Id"}); 
    foreach (var result in results) 
    { 
     Console.WriteLine((result as dynamic).Id); 
    } 

    results = test.GetAllTasks(new[] { "Name", "Description" }); 
    foreach (var result in results) 
    { 
     var dynamicResult=result as dynamic; 
     Console.WriteLine("{0} {1}", dynamicResult.Name, dynamicResult.Description); 
     // The following line will throw an exception 
     //Console.WriteLine((result as dynamic).Id); 
    } 
} 
संबंधित मुद्दे