create generic method for two different anonymous types Function 1:

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

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

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

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

            }

}


Function 2:

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

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

            foreach (var claim in duplicateClaims)
            {
                List&lt;byte?&gt; testIds = new List&lt;byte?&gt;();
                List&lt;string&gt; testNames = new List&lt;string&gt;();
                List&lt;string&gt; resultValues = new List&lt;string&gt;();
                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 =&gt; x.claim_id == claim.claimID).ToList().ForEach(y =&gt; { 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 =&gt; a.claim_id).Where(g =&gt; g.Count() &gt; 1).Select(y =&gt; new { claimID = y.Key, records = y.ToList() });

            var newResult = result.GroupBy(a =&gt; a.claim_id).Select(g =&gt; 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.
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&lt;IValidationQuality&gt; items)
{
    int partitionId = GetPartitionId();
    List &lt; IValidationQuality &gt; = items.Where(x =&gt; x.partition_id == partitionId).ToList();
    var duplicateClaims = result.GroupBy(a =&gt; a.claim_id).Where(g =&gt; g.Count() &gt; 1).Select(y =&gt; new { claimID = y.Key, records = y.ToList() });

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

    foreach (var claim in duplicateClaims)
    {
        List&lt;byte?&gt; testIds = new List&lt;byte?&gt;();
        List&lt;string&gt; testNames = new List&lt;string&gt;();
        List&lt;string&gt; resultValues = new List&lt;string&gt;();
        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 =&gt; x.claim_id == claim.claimID).ToList().ForEach(y =&gt; { 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);