2013-07-18 13 views
7

Linq.Tables (dc.GTMD_Financials) öğesinden UserControl'e veri ekleyen biraz kod oluşturdum. Veritabanındaki her giriş için yeni bir usercontrol gösterir.Çoklu Linq.Yol yönteminde kullanılabilenler

Ancak bu kodu, uygulama boyunca yeniden kullanmak için bir yöntemde kullanmak istiyorum. Benim sorunum her zaman ben veritabanından farklı bir tablo kullanmak istiyorum yöntem (bu nedenle GTMD_Financials değişir)

Ben bunu anlamaya ve herhangi bir yardım veya örnek gerçekten takdir ediyorum.

 int locationControl = 78; 
     DataClasses1DataContext dc = new DataClasses1DataContext(); 

     dc.GTMD_Financials.ToList().ForEach(x => 
     { 
      KPIEntrys uc = new KPIEntrys();   // UserControl 

      uc.KPI = x.KPI;       // Add data to properties 
      uc.Status = x.Status.ToString(); 
      uc.Goal = x.Goal.ToString(); 
      uc.Currently = x.Currently.ToString(); 
      bool checkaction = x.ShowAction == true ? uc.ShowAction = true : uc.ShowAction = false; 
      bool checkstats = x.ShowStats == true ? uc.ShowStats = true : uc.ShowStats = false; 
      bool checkstatus = x.Status < x.StatusSignal ? uc.StatusGood = true : uc.StatusGood = false; 

      uc.Location = new Point(21, locationControl); 
      this.Controls.Add(uc);     // Add Control to Form 

      locationControl = locationControl + 34; 
     } 
     ); 

Bazı şeyler açık değilse lütfen bildirin. Yardımlarınız için şimdiden teşekkür ederiz.

DÜZENLEME: Ben zaten var yardımıyla çalışma almak gibi olamaz

.

int locationControl = 78; 
    DataClasses1DataContext dc = new DataClasses1DataContext(); 

    public List<Control> LoadKPIs(Table<GTMD_Financial> dbTable) 
    { 
     var controls = new List<Control>();    

     dbTable.ToList().ForEach(x => 
     { 
      KPIEntrys uc = new KPIEntrys(); 

      uc.KPI = x.KPI; 
      uc.Status = x.Status.ToString(); 
      uc.Goal = x.Goal.ToString(); 
      uc.Currently = x.Currently.ToString(); 
      uc.ShowAction = (bool)x.ShowAction; 
      uc.ShowStats = (bool)x.ShowStats; 
      uc.StatusGood = x.Status < x.StatusSignal; 
      uc.Location = new Point(21, locationControl); 

      controls.Add(uc); 

      locationControl = locationControl + 34; 
     } 
     ); 
     return controls; 
    } 

yüzden bana sorumu rephrase: i yöntemini çağırdığınızda ı sınıfı değiştirebilir nasıl LoadKPIs (Tablo < Ben zaten var Replys yardımıyla biraz yöntemini düzenlemek başardı GTMD_Financial> DBTable? Yani GTMD_Finacial değişir.

+0

? Yeniden kullanabileceğiniz parçalar ve yeniden kullanamayacağınız parçalar olacak. Yeniden kullanılabilir bir yöntem oluşturmak istiyorsanız bunun çok açık olması gerekir; aslında ne yapıyor? – Maarten

+0

"dc.GTMD_Financials" değişen tek şeydir. Bir dahaki sefere ben "dc.GTMD_Organisation" (ya da başka bir) – Marcel

+0

kullanmak istiyorum sadece ihtiyacınız olan şey, dc.GTMD_Financials –

cevap

4

kullanmak istediğiniz tüm özelliklerini tanımlayan bir arayüz yaz ve ticari kuruluşlar o uygulamak var kullanmak istediğin

public interface IMyReusableInterface { 
    string KPI { get; set; } 
    string Status { get; set; } 
    // etc... 
} 

public partial GTMD_Financials: IMyReusableInterface { 
} 

Şimdi bu arabirim uygulamak nesnelerin listesini kabul eden bir yeniden kullanılabilir bir yöntem yazabilirsiniz.

public List<Control> MyReusableMethod (List<IMyReusableInterface> data) { 
    int locationControl = 78; 
    var controls = new List<Control>(); 

    foreach (var x in data) { 
     KPIEntrys uc = new KPIEntrys();   // UserControl 

     uc.KPI = x.KPI;       // Add data to properties 
     uc.Status = x.Status.ToString(); 
     uc.Goal = x.Goal.ToString(); 
     uc.Currently = x.Currently.ToString(); 
     // I've simplefied the boolean checks. 
     uc.ShowAction = x.ShowAction; 
     uc.ShowStats = x.ShowStats; 
     uc.StatusGood = x.Status < x.StatusSignal; 
     uc.Location = new Point(21, locationControl); 

     controls.Add(uc);     // Add Control to Form 

     locationControl = locationControl + 34; 
    } 

    return controls; 
} 

Ve kullanmak: kod parçalarının yeniden kullanmak istiyoruz

DataClasses1DataContext dc = new DataClasses1DataContext(); 
this.Controls.AddRange(
    MyReusableMethod(
     dc.GTMD_Financials 
      .Cast<IMyReusableInterface>() 
      .ToList() 
    ) 
); 
+0

Thnx, ancak ben işe almak için görünmüyor olabilir ... Muhtemelen benim adına bir hata. "Genel parsiyel GTMD_Financials: IMyReusableInterface { }" oluşturulamıyor gibi görünmüyor ve daha sonra "Denetimler" kullanmaya çalışırken bir hata alıyorum, bulunamadı. – Marcel

+0

Denetimleri Control.ControlCollection olmalıdır, bkz. Http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.aspx. Bunu tamir edeceğim. – Maarten

+0

Ve IMyReusableInterface arabirimini, yöntemle kullanmak istediğiniz sınıflara uygulamalısınız. – Maarten

4

umarım ben bunun doğru

public void myMethod(List<TSource> y) 
int locationControl = 78; 
    y.ForEach(x => 
    { 
     KPIEntrys uc = new KPIEntrys();   // UserControl 

     uc.KPI = x.KPI;       // Add data to properties 
     uc.Status = x.Status.ToString(); 
     uc.Goal = x.Goal.ToString(); 
     uc.Currently = x.Currently.ToString(); 
     bool checkaction = x.ShowAction == true ? uc.ShowAction = true : uc.ShowAction = false; 
     bool checkstats = x.ShowStats == true ? uc.ShowStats = true : uc.ShowStats = false; 
     bool checkstatus = x.Status < x.StatusSignal ? uc.StatusGood = true : uc.StatusGood = false; 

     uc.Location = new Point(21, locationControl); 
     this.Controls.Add(uc);     // Add Control to Form 

     locationControl = locationControl + 34; 
    } 
    ); 
+0

yapıyorlar Tamam, ama bir Linq.Table'ı olarak nasıl kullanırım? Sınırlı bilgilerim için üzgünüm. – Marcel

+0

tamam, nasıl olduğunu biliyorum, aslında TSource kullanmıyorsunuz, yarattığınız bir tür kullanın ve bu türün ihtiyacınız olan tüm özelliklere sahip olduğunu bilin. Bu soyut tip, ya da arayüz de –

+0

olabilir Ben son yorum olarak, @Marcel –

İlgili konular