2013-07-18 11 views
7

Ho creato un po 'di codice che aggiunge dati da Linq.Tables (dc.GTMD_Financials) a un UserControl. Per ogni voce nel database mostra un nuovo usercontrol.Multiple Linq.Tables nel metodo

Ma mi piacerebbe utilizzare questo codice in un metodo per riutilizzarlo in tutta l'applicazione. Il mio problema è che ogni volta che chiamo il metodo vorrei usare una tabella diversa dal database (quindi GTMD_Financials cambia)

Non riesco a capirlo e apprezzerei davvero ogni forma di aiuto o di esempio.

 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; 
     } 
     ); 

Se qualcosa non è chiaro, per favore fatemelo sapere. Grazie in anticipo per qualsiasi aiuto.

EDIT:

io non riesco a farlo funzionare con l'aiuto ho già ottenuto. Sono stato in grado di modificare il metodo un po 'con l'aiuto di replys ho già preso:

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; 
    } 

Così mi permetta di riformulare la mia domanda: come posso cambiare la classe quando chiamo il metodo: LoadKPIs (Tabella < GTMD_Financial> DBTable? cambia così GTMD_Finacial.

+0

Quali pezzi di codice volete riutilizzare? Ci saranno pezzi che puoi riutilizzare e pezzi che non puoi riutilizzare. Devi essere molto chiaro a riguardo se vuoi creare un metodo riutilizzabile; cosa fa in realtà? – Maarten

+0

"dc.GTMD_Financials" è l'unica cosa che cambia. La prossima volta che lo chiamo vorrei usare "dc.GTMD_Organisation" (o un altro) – Marcel

+0

l'unica cosa che vedo è che è necessario ottenere la tabella corrente necessaria per questo metodo invece di dc.GTMD_Financials –

risposta

4

Scrivi un'interfaccia che definisce tutte le proprietà che si desidera utilizzare e implementare che sulle entità aziendali che vuoi usare.

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

public partial GTMD_Financials: IMyReusableInterface { 
} 

Ora è possibile scrivere un metodo riutilizzabile che accetta un elenco di oggetti che implementano tale interfaccia.

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; 
} 

e usarlo:

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

Thnx per l'input, tuttavia non riesco a farlo funzionare .. Probabilmente un errore sul mio conto. Non riesco a creare "pubblico parziale GTMD_Financials: IMyReusableInterface { }" e successivamente ho un errore quando si tenta di utilizzare "Controlli", non può essere trovato. – Marcel

+0

I controlli devono essere Control.ControlCollection, consultare http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.aspx. Lo aggiusterò. – Maarten

+0

E si dovrebbe implementare l'interfaccia IMyReusableInterface sulle classi che si desidera utilizzare con il metodo. – Maarten

4

spera ho capito bene

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

Ok, ma come si usa una Linq.Table come ? Scusa per la mia conoscenza limitata. – Marcel

+0

ok, so come funziona, in realtà non usi TSource, usi un tipo che hai creato e sai che quel tipo ha tutte le proprietà che ti servono. può essere di tipo astratto, o interfaccia anche –

+0

ho dimenticato di segnarti nell'ultimo commento, @Marcel –