如何将两种方法转换为一种方法以提高效率?

发布于 2024-12-04 17:48:55 字数 3855 浏览 0 评论 0原文

如何将两个方法转换为一个方法以提高效率并使代码清晰简洁?

我还有三个这样的条件要检查,但我没有提到(太长了):

 public void getmembershipstatusmembers()
 {
        if (cbGEStatustype.Text != "")
        {
            var totalmembers = from tsgentity in eclipse.members
                               join memtomships in eclipse.membertomships on tsgentity.member_Id equals memtomships.member_Id
                               join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id
                               join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id
                               join mshipstatus in eclipse.mshipstatustypes on memtomships.mshipStatusType_Id equals mshipstatus.mshipStatusType_Id
                               where mshipstatus.mshipStatusType_Name.StartsWith(cbGEStatustype.Text)
                               select new
                               {
                                   tsgentity.member_Id,
                                   tsgentity.member_Lastname,
                                   tsgentity.member_Firstname,
                                   tsgentity.member_Postcode,
                                   tsgentity.member_Reference,
                                   tsgentity.member_CardNum,
                                   tsgentity.member_IsBiometric,
                                   tsgentity.member_Dob,
                                   mshiptypes.mshipType_Name,
                                   mshipstatus.mshipStatusType_Name,
                                   memtomships.memberToMship_EndDate
                               };
            dgvReportMembers.DataSource = totalmembers;       
        }
    }
    public void getcardnumbers()
    {
        if (txtcardnum.Text != "")
        {
            var totalmembers = from tsgentity in eclipse.members
                               join memtomships in eclipse.membertomships on tsgentity.member_Id equals memtomships.member_Id
                               join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id
                               join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id
                               join mshipstatus in eclipse.mshipstatustypes on memtomships.mshipStatusType_Id equals mshipstatus.mshipStatusType_Id
                               where tsgentity.member_CardNum.StartsWith(txtcardnum.Text)
                               select new
                               {
                                   tsgentity.member_Id,
                                   tsgentity.member_Lastname,
                                   tsgentity.member_Firstname,
                                   tsgentity.member_Postcode,
                                   tsgentity.member_Reference,
                                   tsgentity.member_CardNum,
                                   tsgentity.member_IsBiometric,
                                   tsgentity.member_Dob,
                                   mshiptypes.mshipType_Name,
                                   mshipstatus.mshipStatusType_Name,
                                   memtomships.memberToMship_EndDate
                               };
            dgvReportMembers.DataSource = totalmembers;
        }

    }

我正在寻找这样的东西:

 private void allmembers()
 {
       var members = from ......
                          .......
                          .......
        if (cbGEStatustype.Text != "")
        {
           dgvreports.datasource = members.where(.......)
         }
         if (txtcardnum.Text != "")
         {    
             dgvreports.datasource = members.where(.......)
         }
    }

How can I convert two methods into a single method to increase efficiency and make clear and concise code?

I have three more conditions like this to check but I did not mention (that's too long):

 public void getmembershipstatusmembers()
 {
        if (cbGEStatustype.Text != "")
        {
            var totalmembers = from tsgentity in eclipse.members
                               join memtomships in eclipse.membertomships on tsgentity.member_Id equals memtomships.member_Id
                               join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id
                               join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id
                               join mshipstatus in eclipse.mshipstatustypes on memtomships.mshipStatusType_Id equals mshipstatus.mshipStatusType_Id
                               where mshipstatus.mshipStatusType_Name.StartsWith(cbGEStatustype.Text)
                               select new
                               {
                                   tsgentity.member_Id,
                                   tsgentity.member_Lastname,
                                   tsgentity.member_Firstname,
                                   tsgentity.member_Postcode,
                                   tsgentity.member_Reference,
                                   tsgentity.member_CardNum,
                                   tsgentity.member_IsBiometric,
                                   tsgentity.member_Dob,
                                   mshiptypes.mshipType_Name,
                                   mshipstatus.mshipStatusType_Name,
                                   memtomships.memberToMship_EndDate
                               };
            dgvReportMembers.DataSource = totalmembers;       
        }
    }
    public void getcardnumbers()
    {
        if (txtcardnum.Text != "")
        {
            var totalmembers = from tsgentity in eclipse.members
                               join memtomships in eclipse.membertomships on tsgentity.member_Id equals memtomships.member_Id
                               join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id
                               join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id
                               join mshipstatus in eclipse.mshipstatustypes on memtomships.mshipStatusType_Id equals mshipstatus.mshipStatusType_Id
                               where tsgentity.member_CardNum.StartsWith(txtcardnum.Text)
                               select new
                               {
                                   tsgentity.member_Id,
                                   tsgentity.member_Lastname,
                                   tsgentity.member_Firstname,
                                   tsgentity.member_Postcode,
                                   tsgentity.member_Reference,
                                   tsgentity.member_CardNum,
                                   tsgentity.member_IsBiometric,
                                   tsgentity.member_Dob,
                                   mshiptypes.mshipType_Name,
                                   mshipstatus.mshipStatusType_Name,
                                   memtomships.memberToMship_EndDate
                               };
            dgvReportMembers.DataSource = totalmembers;
        }

    }

I am looking for something like this:

 private void allmembers()
 {
       var members = from ......
                          .......
                          .......
        if (cbGEStatustype.Text != "")
        {
           dgvreports.datasource = members.where(.......)
         }
         if (txtcardnum.Text != "")
         {    
             dgvreports.datasource = members.where(.......)
         }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆° 2024-12-11 17:48:55

你找错了树。让一种方法完成其他两种方法的工作有时是一个好主意,但永远不会使您的代码显着提高(或降低)效率。

建议的更改会让您的代码更加清晰吗?这是值得做的。会减少重复吗?这也是值得做的。这是正确的改变吗?查看您的代码,我猜想更好的方法是将方法的公共部分提取到新方法中,并从现有方法中调用该新方法。

You are barking up the wrong tree. Making one method do the job of two other methods is sometimes a good idea, but will never make your code measurably more (or less) efficient.

Will the suggested change make your code more clear? That's worth doing. Will it reduce duplication? That's worth doing, too. Is it the right change to make? Looking at your code, I would guess that a better approach would be to extract the common parts of your methods into a new method, and call that new method from your existing methods.

非要怀念 2024-12-11 17:48:55

如果我正确地阅读了这篇文章,我认为您可以创建您的方法,以便不直接将数据源设置为查询结果,而是将结果作为 IQueryable 对象返回,以便您可以应用尽可能多的后续过滤器你需要。我认为困难可能在于您的两个方法似乎使用匿名类型,该类型在方法之间无法通信,但您可以创建一个简单的类型定义来填补空白。

例如

public class Foo {
public string MemberID { get; set;}
public string LastName { get; set;}
// ... etc.
} 

public IQueryably<Foo> GetUnfiltereredSource()
{
    // NOTE: No WHERE here...
    return 
        from tsgentity in eclipse.members 
        join memtomships in eclipse.membertomships on tsgentity.member_Id equals memtomships.member_Id 
        join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id 
        join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id 
        join mshipstatus in eclipse.mshipstatustypes on memtomships.mshipStatusType_Id equals mshipstatus.mshipStatusType_Id 
        select new Foo
        { 
           MemberID =  tsgentity.member_Id, 
           LastName = tsgentity.member_Lastname, 
           // ... etc.
        }; 
}

public void DoStuff()
{
    dgvreports.datasource = GetUnfilteredSource().Where(x => x.MemberCardNumber.StartsWith(...));

    // OR

    dgvreports.datasource = GetUnfilteredSource().Where(x => x.MemberShipStatusType.StartsWith(...));

    // etc..
}

If I'm reading this correctly, I would think you could create your method so that instead of directly setting the data source to the results of the query, it returns the results as an IQueryable object so that you can apply as many subsequent filters as you need to. The difficulty, I think might be that your two method seem to be using an anonymous type which won't be communicable amongst methods, but you could create a simple type definition to fill the void.

For example

public class Foo {
public string MemberID { get; set;}
public string LastName { get; set;}
// ... etc.
} 

public IQueryably<Foo> GetUnfiltereredSource()
{
    // NOTE: No WHERE here...
    return 
        from tsgentity in eclipse.members 
        join memtomships in eclipse.membertomships on tsgentity.member_Id equals memtomships.member_Id 
        join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id 
        join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id 
        join mshipstatus in eclipse.mshipstatustypes on memtomships.mshipStatusType_Id equals mshipstatus.mshipStatusType_Id 
        select new Foo
        { 
           MemberID =  tsgentity.member_Id, 
           LastName = tsgentity.member_Lastname, 
           // ... etc.
        }; 
}

public void DoStuff()
{
    dgvreports.datasource = GetUnfilteredSource().Where(x => x.MemberCardNumber.StartsWith(...));

    // OR

    dgvreports.datasource = GetUnfilteredSource().Where(x => x.MemberShipStatusType.StartsWith(...));

    // etc..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文