Riccardo Pezzolati
Riccardo Pezzolati

Reputation: 359

How to unify these two methods

I have these two presenters:

public class Presenter
{
    public int? Society { get; set; }

    public int? Code { get; set; }

    public string BusinessName { get; set; }

    public string Address { get; set; }

    public string VatNumber { get; set; }
        
    public bool ADR {get;set;}

    public string IdTransaction { get; set; }
}
    
public class OtherPresenter : Presenter
{
    public int? OtherCode { get; set; }

    public string BusinessNameOther { get; set; }

    public string AddressOther { get; set; }

    public string VatNumberOther { get; set; }
}

The two presenters are populated by a search on DB (same tables involved but with different select fields)

public IEnumerable<Presenter> GetPresenter(DataInput input)
    {
        return this.Context.PersonalData.Join(
                            this.Context.IDs,
                            s => new { field1 = s.IDIndex, field2 = s.ADR },
                            h => new { field1 = h.ID, field2 = h.ADR },
                            (s, h) => new { details = s, ids = h })
                        .Where(x =>
                               (!input.Society.HasValue || x.ids.Society == input.Society) &&
                               (input.BusinessName.StringIsNullOrEmpty() || x.details.BusinessName == input.BusinessName) &&
                               (input.Address.StringIsNullOrEmpty() || x.details.Address == input.Address) &&
                               (input.VatNumber.StringIsNullOrEmpty() || x.details.VAT == input.VatNumber) &&
                               (input.BusinessNameOther.StringIsNullOrEmpty() || x.details.Other == input.BusinessNameOther) &&
                               (input.AddressOther.StringIsNullOrEmpty() || x.details.OtherAddress == input.AddressOther) &&
                               (input.VatNumberOther.StringIsNullOrEmpty() || x.details.OtherVAT == input.VatNumberOther) &&
                               x.details.ADR == Enums.Flag(input.AdrFlag)
                        .Select(x => new Presenter()
                        {
                            Society = x.ids.Society ?? null,
                            Code = x.ids.Code ?? null,
                            BusinessName = x.details.BusinessName,
                            Address = x.details.Address,
                            VatNumber = x.details.VAT,
                            ADR = x.ids.ADR,
                            IdTransaction = input.IDTransaction
                        });
    }
    
    public IEnumerable<OtherPresenter> GetOtherPresenter(DataInput input)
    {

        return this.Context.PersonalData.Join(
                            this.Context.IDs,
                            s => new { field1 = s.IDIndex, field2 = s.ADR },
                            h => new { field1 = h.ID, field2 = h.ADR },
                            (s, h) => new { details = s, ids = h })
                        .Where(x =>
                               (!input.Society.HasValue || x.ids.Society == input.Society) &&
                               (input.BusinessName.StringIsNullOrEmpty() || x.details.BusinessName == input.BusinessName) &&
                               (input.Address.StringIsNullOrEmpty() || x.details.Address == input.Address) &&
                               (input.VatNumber.StringIsNullOrEmpty() || x.details.VAT == input.VatNumber) &&
                               (input.BusinessNameOther.StringIsNullOrEmpty() || x.details.Other == input.BusinessNameOther) &&
                               (input.AddressOther.StringIsNullOrEmpty() || x.details.OtherAddress == input.AddressOther) &&
                               (input.VatNumberOther.StringIsNullOrEmpty() || x.details.OtherVAT == input.VatNumberOther) &&
                               x.details.ADR == Enums.Flag(input.AdrFlag)
                        .Select(x => new OtherPresenter()
                        {
                            Society = x.ids.Society ?? null,
                            Code = x.ids.Code ?? null,
                            BusinessName = x.details.BusinessName,
                            Address = x.details.Address,
                            VatNumber = x.details.VAT,
                            OtherCode = x.ids.DestinationOtherCode ?? null,
                            BusinessNameOther = x.details.Other,
                            AddressOther = x.details.OtherAddress,
                            VatNumberOther = x.details.OtherVAT,
                            ADR = x.ids.ADR,
                            IdTransaction = input.IDTransaction
                        });
    }

I tried to assign the query to a variable without the select, the variable is of type IOrderedQueryable new {PersonalDataModel details, IDsModel ids} and then apply the select later, it is possible to execute the query and then, based on the caller, apply a select or the other since "a" is an Anonymous Types?

UPDATE Found the solution instead of var query = I did IEnumerable<dynamic> query = my_query_LINQ and then extracted with the select depending on the caller

Upvotes: 0

Views: 65

Answers (1)

SomeBody
SomeBody

Reputation: 8743

Since the Join and the Where part are the same for both methods, you can store the result of your Where in a variable. The you check whether you want your normal or your other result and use the Select that you need for your choice.

 public IEnumerable<Presenter> GetPresenter(DataInput input, bool other)
 {
      var query = this.Context.PersonalData.Join(
                      this.Context.IDs,
                      s => new { field1 = s.IDIndex, field2 = s.ADR },
                      h => new { field1 = h.ID, field2 = h.ADR },
                      (s, h) => new { details = s, ids = h })
                   .Where(x =>
                      (!input.Society.HasValue || x.ids.Society == input.Society) &&
                      (input.BusinessName.StringIsNullOrEmpty() || x.details.BusinessName == input.BusinessName) &&
                      (input.Address.StringIsNullOrEmpty() || x.details.Address == input.Address) &&
                      (input.VatNumber.StringIsNullOrEmpty() || x.details.VAT == input.VatNumber) &&
                      (input.BusinessNameOther.StringIsNullOrEmpty() || x.details.Other == input.BusinessNameOther) &&
                      (input.AddressOther.StringIsNullOrEmpty() || x.details.OtherAddress == input.AddressOther) &&
                      (input.VatNumberOther.StringIsNullOrEmpty() || x.details.OtherVAT == input.VatNumberOther) &&  x.details.ADR == Enums.Flag(input.AdrFlag);

      if(other)
      {
         return query.Select(x => new OtherPresenter()
            {
                Society = x.ids.Society ?? null,
                Code = x.ids.Code ?? null,
                BusinessName = x.details.BusinessName,
                Address = x.details.Address,
                VatNumber = x.details.VAT,
                OtherCode = x.ids.DestinationOtherCode ?? null,
                BusinessNameOther = x.details.Other,
                AddressOther = x.details.OtherAddress,
                VatNumberOther = x.details.OtherVAT,
                ADR = x.ids.ADR,
                IdTransaction = input.IDTransaction
            });
      }
      else
      {
          return query.Select(x => new Presenter()
          {
                Society = x.ids.Society ?? null,
                Code = x.ids.Code ?? null,
                BusinessName = x.details.BusinessName,
                Address = x.details.Address,
                VatNumber = x.details.VAT,
                ADR = x.ids.ADR,
                IdTransaction = input.IDTransaction
           });
       }
    }

Upvotes: 1

Related Questions