C# 方法中的多个Linq.表

C# 方法中的多个Linq.表,c#,winforms,linq,C#,Winforms,Linq,我创建了一些代码,将Linq.Tables(dc.GTMD_Financials)中的数据添加到用户控件中。对于数据库中的每个条目,它都显示一个新的usercontrol 但是我想在一种方法中使用这段代码,以便在整个应用程序中重用它。我的问题是,每次调用该方法时,我都希望使用不同于数据库的表(因此GTMD_Financials会发生变化) 我似乎无法理解,非常感谢任何形式的帮助或示例 int locationControl = 78; DataClasses1Da

我创建了一些代码,将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=新DataClasses1DataContext();
dc.GTMD_Financials.ToList().ForEach(x=>
{
KPIEntrys uc=new KPIEntrys();//用户控件
uc.KPI=x.KPI;//将数据添加到属性
uc.Status=x.Status.ToString();
uc.Goal=x.Goal.ToString();
uc.current=x.current.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
如果有不清楚的地方,请告诉我。 提前感谢您的帮助

编辑:

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

    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;
    }
int-locationControl=78;
DataClasses1DataContext dc=新DataClasses1DataContext();
公共列表加载KPI(表dbTable)
{
var controls=新列表();
dbTable.ToList().ForEach(x=>
{
kPrientrys uc=新kPrientrys();
uc.KPI=x.KPI;
uc.Status=x.Status.ToString();
uc.Goal=x.Goal.ToString();
uc.current=x.current.ToString();
uc.ShowAction=(bool)x.ShowAction;
uc.ShowStats=(bool)x.ShowStats;
uc.StatusGood=x.Status

因此,让我重新表述我的问题:当我调用LoadKPIs(Table dbTable?So GTMD_financial changes)方法时,如何更改类。

希望我做对了

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;
    }
    );
public void myMethod(列表y)
int locationControl=78;
y、 ForEach(x=>
{
KPIEntrys uc=new KPIEntrys();//用户控件
uc.KPI=x.KPI;//将数据添加到属性
uc.Status=x.Status.ToString();
uc.Goal=x.Goal.ToString();
uc.current=x.current.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
编写一个定义所有要使用的属性的接口,并在要使用的业务实体上实现该接口

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;
}
公共列表MyReusableMethod(列表数据){
int locationControl=78;
var controls=新列表();
foreach(数据中的变量x){
KPIEntrys uc=new KPIEntrys();//用户控件
uc.KPI=x.KPI;//将数据添加到属性
uc.Status=x.Status.ToString();
uc.Goal=x.Goal.ToString();
uc.current=x.current.ToString();
//我简化了布尔检查。
uc.ShowAction=x.ShowAction;
uc.ShowStats=x.ShowStats;
uc.StatusGood=x.Status
并使用它:

DataClasses1DataContext dc = new DataClasses1DataContext();
this.Controls.AddRange(
    MyReusableMethod(
        dc.GTMD_Financials
            .Cast<IMyReusableInterface>()
            .ToList()
    )
);
DataClasses1DataContext dc=newdataclasses1datacontext();
this.Controls.AddRange(
MyReusableMethod(
dc.GTMD_财务部
.Cast()
托利斯先生()
)
);

您希望重用哪些代码?有些代码您可以重用,有些代码您不能重用。如果您想创建一个可重用的方法,您必须非常清楚这一点;它实际上是做什么的?“dc.GTMD_Financials”是唯一会改变的东西。下次我称之为“dc.GTMD_organization”(或另一个)我看到您需要的唯一一件事是将当前所需的表获取到此方法,而不是dc.GTMD_financials是的,但这是我似乎无法理解的事情。所有表是否都包含属性KPI、状态、目标、当前、ShowAction、ShowStats、StatusSignal?好的,但是我如何使用Linq.表?抱歉,我知道的有限没关系,我知道感觉如何,实际上你不使用TSource,你使用你创建的类型,并且知道该类型具有你需要的所有属性。它可以是抽象类型,也可以是接口。我很难创建一个包含所有内容的类型。它必须类似于:公共类{内部字符串KPI}或者我完全错了。对这些东西来说有点陌生。@Marcel你需要我给你一个样品吗?我会非常感激的!谢谢你的输入,但是我似乎无法让它工作..可能是