5

我创建了一些代码,将 Linq.Tables (dc.GTMD_Financials) 中的数据添加到 UserControl。对于数据库中的每个条目,它都会显示一个新的用户控件。

但是我想在一个方法中使用这个代码来在整个应用程序中重用它。我的问题是,每次我调用该方法时,我都想使用数据库中的不同表(因此 GTMD_Financials 更改)

我似乎无法弄清楚,并且非常感谢任何形式的帮助或示例。

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

如果有不清楚的地方,请告诉我。提前感谢您的帮助。

编辑:

在我已经得到的帮助下,我似乎无法让它工作。在我已经得到的回复的帮助下,我能够稍微编辑该方法:

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

所以让我重新表述我的问题:当我调用方法时如何更改类:LoadKPIs(Table< GTMD_Financial > dbTable?所以 GTMD_Finacial 发生了变化。

4

2 回答 2

4

编写一个接口来定义您想要使用的所有属性,并在您想要使用的业务实体上实现它。

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

public partial GTMD_Financials: IMyReusableInterface {
}

现在您可以编写一个可重用的方法,该方法接受实现该接口的对象列表。

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

并使用它:

DataClasses1DataContext dc = new DataClasses1DataContext();
this.Controls.AddRange(
    MyReusableMethod(
        dc.GTMD_Financials
            .Cast<IMyReusableInterface>()
            .ToList()
    )
);
于 2013-07-18T11:15:34.737 回答
2

希望我做对了

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;
    }
    );
于 2013-07-18T11:09:56.230 回答