开发者

How can I convert two methods into a single method to increase efficiency?

开发者 https://www.devze.com 2023-04-05 02:30 出处:网络
How can I convert two methods into a single method to increase efficiency and make clear and concise code?

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(.......)
         }
    }


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.


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..
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号