0
\$\begingroup\$

Function 1:

 public HttpResponseMessage Get340BValidations()
        {
            int partitionId = GetPartitionId();
            //int partitionId = 10054;
            this.db.Database.CommandTimeout = 180;
            List<Validation340B> result = this.db.VW_Validation_340B.Where(x => x.partition_id == partitionId).ToList();

var duplicateClaims = result.GroupBy(a => a.claim_id).Where(g => g.Count() > 1).Select(y => new { claimID = y.Key, records = y.ToList() });

            var newResult = result.GroupBy(a => a.claim_id).Select(g => g.First()).ToList();

            foreach (var claim in duplicateClaims)
            {
                List<byte?> testIds = new List<byte?>();
                foreach (var record in claim.records)
                {
                    testIds.Add(record.claim_validation_test_id[0]);
                }
                newResult.Where(x => x.claim_id == claim.claimID).ToList().ForEach(y => y.claim_validation_test_id = testIds);

            }

}

Function 2:

public HttpResponseMessage GetQuantityValidations()
        {
            int partitionId = GetPartitionId();
            List<ValidationQuantity> = this.db.VW_Validation_Quantity.Where(x => x.partition_id == partitionId).ToList();
var duplicateClaims = result.GroupBy(a => a.claim_id).Where(g => g.Count() > 1).Select(y => new { claimID = y.Key, records = y.ToList() });

            var newResult = result.GroupBy(a => a.claim_id).Select(g => g.First()).ToList();

            foreach (var claim in duplicateClaims)
            {
                List<byte?> testIds = new List<byte?>();
                List<string> testNames = new List<string>();
                List<string> resultValues = new List<string>();
                foreach (var record in claim.records)
                {
                    testIds.Add(record.claim_validation_test_id[0]);
                    testNames.Add(record.Test[0]);
                    resultValues.Add(record.Result[0]);
                }

                newResult.Where(x => x.claim_id == claim.claimID).ToList().ForEach(y => { y.claim_validation_test_id = testIds; y.Test = testNames; y.Result = resultValues; });
}
            }

Two functions contains same set of code for finding the duplicate records and modification below part

var duplicateClaims = result.GroupBy(a => a.claim_id).Where(g => g.Count() > 1).Select(y => new { claimID = y.Key, records = y.ToList() });

            var newResult = result.GroupBy(a => a.claim_id).Select(g => g.First()).ToList();

            foreach (var claim in duplicateClaims)
            {
                ........
}

how can i simplify above two functions which uses the same set of code for finding the duplicates. need one common function which should behave like generic method accept different model as parameter and implement the same functionalities.

\$\endgroup\$
  • 2
    \$\begingroup\$ Is this from the VW's tool that overrides the emission prameters of the engine? :-P \$\endgroup\$ – t3chb0t Jun 15 '16 at 11:08
  • \$\begingroup\$ @T3CHB0T i am not sure what VW's tool mean \$\endgroup\$ – SivaRajini Jun 15 '16 at 11:10
  • \$\begingroup\$ @SivaRajini TheDefeatDevice \$\endgroup\$ – forsvarir Jun 15 '16 at 11:15
  • \$\begingroup\$ T3chb0t is making a joke about Volkswagen's cheating on diesel emissions tests. \$\endgroup\$ – 200_success Jun 15 '16 at 11:16
  • 1
    \$\begingroup\$ Joking aside, please tell us what these functions actually do, and make that the title of the question. (See How to Ask.) \$\endgroup\$ – 200_success Jun 15 '16 at 11:20
2
\$\begingroup\$

If you want to have only one method you need to do the following:

Create an interface for properties that are common for both models like:

public interface IValidationQuality
{
    int partition_id { get; }
    int claim_id { get; }
    int claim_validation_test_id { get; }
}

(those are just few of them, you need to find the others)

Add this interface to both models:

public class VW_Validation_Quantity : IValidationQuality { }
public class VW_Validation_340B : IValidationQuality { }

finally you make a few small adjustments to one of the methods:

public HttpResponseMessage GetValidations(ICollection<IValidationQuality> items)
{
    int partitionId = GetPartitionId();
    List < IValidationQuality > = items.Where(x => x.partition_id == partitionId).ToList();
    var duplicateClaims = result.GroupBy(a => a.claim_id).Where(g => g.Count() > 1).Select(y => new { claimID = y.Key, records = y.ToList() });

    var newResult = result.GroupBy(a => a.claim_id).Select(g => g.First()).ToList();

    foreach (var claim in duplicateClaims)
    {
        List<byte?> testIds = new List<byte?>();
        List<string> testNames = new List<string>();
        List<string> resultValues = new List<string>();
        foreach (var record in claim.records)
        {
            testIds.Add(record.claim_validation_test_id[0]);
            testNames.Add(record.Test[0]);
            resultValues.Add(record.Result[0]);
        }

        newResult.Where(x => x.claim_id == claim.claimID).ToList().ForEach(y => { y.claim_validation_test_id = testIds; y.Test = testNames; y.Result = resultValues; });
    }
}

Which means that you pass each collection to this method and becasue both models share the same interface it can work with both of them.

Then you can call it like this:

var result1 = GetValidations(this.db.VW_Validation_Quantity);
var result2 = GetValidations(this.db.VW_Validation_340B);
\$\endgroup\$
  • \$\begingroup\$ properties defined in interface must be implemented in inherited classes right ? \$\endgroup\$ – SivaRajini Jun 15 '16 at 12:15
  • 1
    \$\begingroup\$ @SivaRajini right, but they already are implemented there so your job is to only create the interface and add it to both models as they already have the same properties \$\endgroup\$ – t3chb0t Jun 15 '16 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.