Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Linq.Tables in method

Tags:

c#

linq

winforms

I've created a little bit of code which adds data from Linq.Tables (dc.GTMD_Financials) to a UserControl. For every entry in the database it shows a new usercontrol.

But i would like to use this code in a method to reuse it throughout the application. My problem is that each time i call the method i would like to use a different table from the database (so GTMD_Financials changes)

I can't seem to figure it out and would really appreciate any form of help or example.

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

If something is unclear please let me know. Thanks in advance for any help.

EDIT:

I can't seem to get it working with the help i already got. I was able to edit the method a little bit with the help from replys i already got:

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

So let me rephrase my question: How can i change the class when i call the method: LoadKPIs(Table<GTMD_Financial> dbTable? So GTMD_Finacial changes.

like image 865
Marcel Avatar asked Jul 18 '13 11:07

Marcel


2 Answers

Write an interface which defines all the properties you want to use, and implement that on the business entities that you want to use.

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

public partial GTMD_Financials: IMyReusableInterface {
}

Now you can write a reusable method which accepts a list of objects which implement that interface.

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

And use it:

DataClasses1DataContext dc = new DataClasses1DataContext();
this.Controls.AddRange(
    MyReusableMethod(
        dc.GTMD_Financials
            .Cast<IMyReusableInterface>()
            .ToList()
    )
);
like image 102
Maarten Avatar answered Nov 09 '22 12:11

Maarten


hopefully i got it right

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;
    }
    );
like image 31
No Idea For Name Avatar answered Nov 09 '22 13:11

No Idea For Name