2013-06-18 7 views
5

निदर्शी प्रयोजनों के लिए मैं Certifications संपत्तिएक संपत्ति

public int EmployeeId { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 

     private List<string> certifications = new List<string>(); 
     public List<string> Certifications 
     { 
      get { return certifications; } 
      set { certifications = value; } 
     } 

public List<string> RemoveDuplicates(List<string> s) 
     { 
      List<string> dupesRemoved = s.Distinct().ToList(); 
      foreach(string str in dupesRemoved) 
       Console.WriteLine(str); 
      return dupesRemoved; 
     } 

में अनेक गतिविधियां दूर करने के लिए कई क्षेत्रों और एक विधि के साथ एक सरल कर्मचारी वर्ग मिल गया है के रूप में एक सूची साथ वस्तुओं की एक सूची से अद्वितीय मान रही RemoveDuplicates विधि कर्मचारी ऑब्जेक्ट की प्रमाणन प्रॉपर्टी में किसी भी डुप्लिकेट स्ट्रिंग को हटा देगा। अब विचार करें कि मेरे पास कर्मचारी वस्तुओं की एक सूची है या नहीं।

Employee e = new Employee(); 
      List<string> stringList = new List<string>(); 
      stringList.Add("first"); 
      stringList.Add("second"); 
      stringList.Add("third"); 
      stringList.Add("first"); 
      e.Certifications = stringList; 
      // e.Certifications = e.RemoveDuplicates(e.Certifications); works fine 

      Employee e2 = new Employee(); 
      e2.Certifications.Add("fourth"); 
      e2.Certifications.Add("fifth"); 
      e2.Certifications.Add("fifth"); 
      e2.Certifications.Add("sixth"); 

      List<Employee> empList = new List<Employee>(); 
      empList.Add(e); 
      empList.Add(e2); 

मैं

foreach (Employee emp in empList) 
      { 
       emp.Certifications = emp.RemoveDuplicates(emp.Certifications); 
      } 

सूची में उपयोग सभी अद्वितीय प्रमाणपत्र की एक सूची प्राप्त करने के लिए कर सकता है, सभी कर्मचारियों से लेकिन मैं

stringList = empList.Select(emp => emp.Certifications.Distinct().ToList()); 
के लिए कुछ समान LINQ में ऐसा करने के लिए, चाहते हैं

यह मुझे एक त्रुटि देता है

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'. An explicit conversion exists (are you missing a cast?) 

मैं कर्मचारी वस्तुओं की सूची से अद्वितीय प्रमाणन की सूची कैसे प्राप्त कर सकता हूं?

+1

यदि कर्मचारी आईडी एक अद्वितीय पहचानकर्ता है तो मैं आपको GetHashCode का उपयोग करके GetHashCode और बराबर ओवरराइड करने की सलाह देता हूं। कर्मचारी में डुप्लिकेट की अनुमति न देने के लिए सूची प्रमाणीकरण एक हैशसेट (सूची नहीं) भी बनाएं। – Paparazzi

+0

यह केवल चित्रकारी उद्देश्यों के लिए है, लेकिन इसे इंगित करने के लिए धन्यवाद, मैं टूल सेट में इसे एक फ़ाइल कर दूंगा। – wootscootinboogie

उत्तर

18

यदि मैं समझता हूं, तो आप सभी कर्मचारियों के बीच सभी अद्वितीय प्रमाणपत्रों की एक सूची चाहते हैं। जो आप उप-सूचियों को चुन सकते हैं, लेकिन उन्हें एक चपटा रूप में देता है

var uniqueCerts = empList.SelectMany(e => e.Certifications).Distinct().ToList(); 
+1

मुझे पता था कि मुझे करीब होना था, और यह एक लाइनर था :) – wootscootinboogie

3

आप SelectMany उपयोग करना चाहते हैं,:

stringList = empList.SelectMany(emp => emp.Certifications).Distinct().ToList(); 
0

आप SelectMany एक्सटेंशन का उपयोग कर कोशिश कर सकते हैं यह SelectMany के लिए एक नौकरी हो सकता है?

var employeeCertifications = (from e in employeeList select e.Certifications).SelectMany(x => x).Distinct(); 
संबंधित मुद्दे