C# 将控件绑定到LINQ的重构方法

C# 将控件绑定到LINQ的重构方法,c#,.net,winforms,linq,refactoring,C#,.net,Winforms,Linq,Refactoring,我有一个函数将LINQ结果绑定到表单上的控件。下面的代码可以工作,但我就是无法克服我在复制/粘贴方面应该挨耳光的感觉。有人能帮我除臭吗 谢谢大家! private void BindDataToForm() { // Bind data to form CaseNotesDataContext db = new CaseNotesDataContext(); Table<CN_MaintItem> caseNotesItems

我有一个函数将LINQ结果绑定到表单上的控件。下面的代码可以工作,但我就是无法克服我在复制/粘贴方面应该挨耳光的感觉。有人能帮我除臭吗

谢谢大家!

private void BindDataToForm()
    {
        // Bind data to form
        CaseNotesDataContext db = new CaseNotesDataContext();
        Table<CN_MaintItem> caseNotesItems = db.GetTable<CN_MaintItem>();

        // For each object
        var contactType = from cType in caseNotesItems
                          where cType.CategoryID == 2
                          select cType.ItemDescription;
        chkContactType.DataSource = contactType;

        var contactLocation = from cLocation in caseNotesItems
                          where cLocation.CategoryID == 3
                          select cLocation.ItemDescription;
        lkuContactLocation.Properties.DataSource = contactLocation;

        var contactMethod = from cMethod in caseNotesItems
                          where cMethod.CategoryID == 4
                          select cMethod.ItemDescription;
        lkuContactMethod.Properties.DataSource = contactMethod;

        var contactWith = from cWith in caseNotesItems
                          where cWith.CategoryID == 5
                          select cWith.ItemDescription;
        chkContactWith.DataSource = contactWith;

        var domains = from d in caseNotesItems
                          where d.CategoryID == 6
                          select d.ItemDescription;
        chkDomains.DataSource = domains;
    }
private void BindDataToForm()
{
//将数据绑定到表单
CaseNotesDataContext db=新的CaseNotesDataContext();
表caseNotesItems=db.GetTable();
//对于每个对象
var contactType=来自caseNotesItems中的cType
其中cType.CategoryID==2
选择cType.itemsdescription;
chkContactType.DataSource=联系人类型;
var contactLocation=来自caseNotesItems中的cLocation
其中cLocation.CategoryID==3
选择cLocation.itemsdescription;
lkuContactLocation.Properties.DataSource=联系人位置;
var contactMethod=来自caseNotesItems中的CMMethod
其中cMethod.CategoryID==4
选择cMethod.itemsdescription;
lkuContactMethod.Properties.DataSource=contactMethod;
var contactWith=来自caseNotesItems中的CWWITH
其中cWith.CategoryID==5
选择cWith.itemsdescription;
chkContactWith.DataSource=contactWith;
var domains=来自caseNotesItems中的d
其中d.CategoryID==6
选择d.ItemDescription;
chkDomains.DataSource=域;
}

您可以通过执行以下操作来“缩短”它:

IQueryable<string> GetDescriptions(int cat)
{
  return from x in caseNotesItems
         where x.CategoryID == cat
         select x.ItemDescription;

}

您可以通过执行以下操作来“缩短”它:

IQueryable<string> GetDescriptions(int cat)
{
  return from x in caseNotesItems
         where x.CategoryID == cat
         select x.ItemDescription;

}

我不知道这是否真的解决了任何问题,但请尝试:

public static class MyExtentsions {
    public IQueryable<string> GetItemDescriptions(this Table<CN_MaintItem> table, int cat)
    {
        return from x in table
               where x.CategoryID == cat
               select x.ItemDescription;
    }
}
公共静态类MyExtensions{
公共IQueryable GetItemDescriptions(此表,int cat)
{
从表中的x返回
其中x.CategoryID==cat
选择x.ItemDescription;
}
}
因此,您可以像这样提取信息:

using (CaseNotesDataContext db = new CaseNotesDataContext()) {
    Table<CN_MaintItem> caseNotesItems = db.GetTable<CN_MaintItem>();

    chkContactType.DataSource = caseNotesItems.GetItemDescriptions(2);
    lkuContactLocation.Properties.DataSource = caseNotesItems.GetItemDescriptions(3);
    // etc... 
}
使用(CaseNotesDataContext db=new CaseNotesDataContext()){
表caseNotesItems=db.GetTable();
chkContactType.DataSource=caseNotesItems.GetItemDescriptions(2);
lkuContactLocation.Properties.DataSource=caseNotesItems.GetItemDescriptions(3);
//等等。。。
}

我不知道这是否真的解决了任何问题,但请尝试:

public static class MyExtentsions {
    public IQueryable<string> GetItemDescriptions(this Table<CN_MaintItem> table, int cat)
    {
        return from x in table
               where x.CategoryID == cat
               select x.ItemDescription;
    }
}
公共静态类MyExtensions{
公共IQueryable GetItemDescriptions(此表,int cat)
{
从表中的x返回
其中x.CategoryID==cat
选择x.ItemDescription;
}
}
因此,您可以像这样提取信息:

using (CaseNotesDataContext db = new CaseNotesDataContext()) {
    Table<CN_MaintItem> caseNotesItems = db.GetTable<CN_MaintItem>();

    chkContactType.DataSource = caseNotesItems.GetItemDescriptions(2);
    lkuContactLocation.Properties.DataSource = caseNotesItems.GetItemDescriptions(3);
    // etc... 
}
使用(CaseNotesDataContext db=new CaseNotesDataContext()){
表caseNotesItems=db.GetTable();
chkContactType.DataSource=caseNotesItems.GetItemDescriptions(2);
lkuContactLocation.Properties.DataSource=caseNotesItems.GetItemDescriptions(3);
//等等。。。
}

诀窍在于存储明确绑定控件所需的信息。您可以创建一个
IDictionary
来存储每个控件的类别id。然后迭代所有控件,在字典中查找id,最后在条目存在时绑定它。您可以通过创建一个以委托为值的字典来扩展此方法。这允许您对不同的控件执行不同的查询。此外,您还可以使用
Control.Tag
属性来存储此信息。

技巧是存储显式绑定控件所需的信息。您可以创建一个
IDictionary
来存储每个控件的类别id。然后迭代所有控件,在字典中查找id,最后在条目存在时绑定它。您可以通过创建一个以委托为值的字典来扩展此方法。这允许您对不同的控件执行不同的查询。此外,您还可以使用
Control.Tag
属性来存储此信息。

您根本没有传递表。很好,这会产生很大的差异,为什么要努力制作实际的C,然后将其称为伪代码?实例变量,这是一个多么新颖的想法!我从来没有说过这是伪代码,也不打算用勺子喂食或只是给出复制/粘贴答案。很抱歉,如果它打扰了你。你根本没有传递这个表。好吧,这会有很大的不同,为什么要努力制作实际的C#,然后称之为伪代码?实例变量,多么新颖的想法!我从来没有说过这是伪代码,也不打算用勺子喂食或只是给出复制/粘贴答案。对不起,如果打扰了你。谢谢,这似乎是正确的。但我的不喜欢“这个”。“扩展方法只能在非泛型、非嵌套的静态类中声明”的想法??是的,您需要在静态类中声明它。公共静态类MyExtensions{(放在这里)}谢谢,这似乎是正确的。但我的不喜欢“这个”。“扩展方法只能在非泛型、非嵌套的静态类中声明”的想法??是的,您需要在静态类中声明它。公共静态类MyExtensions{(放在这里)}