2012-01-18 23 views
5

मैंने एक एमवीसी अनुप्रयोग के लिए निम्नलिखित दृश्य मॉडल बनाया है। मैं जो करना चाहता हूं वह इसे IEnumerable वर्ग बना देता है ताकि मैं foreach कथन का उपयोग करके अपने पृष्ठ में डेटा के माध्यम से पुन: प्रयास कर सकूं।मैं इस कक्षा को एक आईनेमरेबल कैसे बना सकता हूं?

public class EstimateDetailsModel 
{ 
    public string dma { get; set; } 
    public string callsign { get; set; } 
    public string description { get; set; } 

} 

मामले में यह प्रासंगिक है है यहाँ, मेरे भंडार में इसी LINQ क्वेरी कि EstimatesDetailsModel वर्ग को दर्शाता है:

public IEnumerable<EstimateDetailsModel> GetEstimateDetails(int id) 
{ 
    var estimateDetails = from e in db.Estimates 
          join es in db.EstimateStations on e.EstimateID equals es.EstimateID 
          join s in db.Stations on es.StationID equals s.StationID 
          join m in db.Markets on s.MarketID equals m.MarketID 
          where e.EstimateID == 1 
          select new EstimateDetailsModel { dma = m.DmaName, callsign = s.CallSign, description = s.StationDescription }; 
    return estimateDetails;         
} 
+0

क्या आप इसका उदाहरण दिखा सकते हैं कि आप इसका उपयोग कैसे करना चाहते हैं? क्या आपका मतलब यह है कि मॉडल के गुणों पर यह पुनरावृत्त है? –

+0

आप वास्तव में IENumerable बनाना चाहते हैं? EstimateDetailsModel की गुण? –

+0

सब ठीक दिखता है। आपको 'GetEstimateDetails()' के परिणामों के माध्यम से पुन: प्रयास करने में सक्षम होना चाहिए। – Humberto

उत्तर

15

शायद आप कुछ इस तरह की तलाश में हैं:

public class EstimateDetailsModel : IEnumerable<string> 
{ 
    public string Dma { get; set; } 

    public string Callsign { get; set; } 

    public string Description { get; set; } 

    public IEnumerator<string> GetEnumerator() 
    { 
     yield return Dma; 
     yield return Callsign; 
     yield return Description; 
    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return this.GetEnumerator(); 
    } 
} 

(मैं गुण पूंजीकृत किया है, जो सामान्य है शैली।)

+0

नहीं पता था कि आप 'आईन्यूमेरेटर' विधि में नग्न 'उपज वापसी 'कर सकते हैं। अच्छा लगा। –

0

यह आप की तरह लगता है इस तरह कुछ करने के लिए प्रयास कर रहे हों?

public class EstimateDetailsModel: List<EstimateDetailsModel.EstimateDetail> 
{ 
    public class EstimateDetail 
    { 
     public string dma { get; set; } 
     public string callsign { get; set; } 
     public string description { get; set; } 
    } 
} 
0

क्यों सिर्फ EstimateDetailsModel कि भंडार से EstimateDetails को उजागर करता है में एक संपत्ति का उपयोग नहीं? कुछ इस तरह, यह मानते हुए आप मॉडल में एक भंडार संदर्भ होगा:

public class EstimateDetailsModel 
{ 
    public int EstimateID { get; set; } 

    public IEnumerable<EstimateDetailsModel> EstimateDetails 
    { 
     return Repository.GetEstimateDetails(this.EstimateID); 
    } 
} 
0

प्रतिबिंब के साथ किसी भी ऑब्जेक्ट के गुणों के नामों और मानों की गणना स्वचालित करना संभव है (this question देखें, others के बीच)। लेकिन यह कभी-कभी ओवरकिल होता है। मुझे लगता है कि आप EstimateDetailsModel को एक शब्दकोश के रूप में देखने में रुचि रखते हैं। मैं निम्नलिखित का सुझाव देता हूं:

public class EstimateDetailsModel : IEnumerable<KeyValuePair<string, string>> 
{ 
    public string Dma { get; set; } 
    public string CallSign { get; set; } 
    public string Description { get; set; } 

    public IEnumerator<KeyValuePair<string, string>> GetEnumerator() 
    { 
     yield return new KeyValuePair<string, string>("Dma", Dma); 
     yield return new KeyValuePair<string, string>("CallSign", CallSign); 
     yield return new KeyValuePair<string, string>("Description", Description); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 
} 
संबंधित मुद्दे