How to write a LINQ query to retrieve individual records based on only specific properties?

I have an array of DataTestplans objects from which I am trying to get records for specific DataID and ProductID using the specified LINQ query, my current query has Distinct() , which differs by all 5 mentioned properties, how to get different records based on the DataID , TestPlanName , TCIndexList and ProductID ?

DataTestplans: -

 [ { "DataTestPlanID": 0, "DataID": 19148, "TestPlanName": "string", "TCIndexList": "string", "ProductID": 2033915 }, { "DataTestPlanID": 0, "DataID": 19148, "TestPlanName": "string", "TCIndexList": "string", "ProductID": 2033915 }, { "DataTestPlanID": 0, "DataID": 19149, "TestPlanName": "string", "TCIndexList": "string", "ProductID": -2642 } ] 

LINQ

  DataTestPlans_DataID_ProductID = DataTestPlans.Where(c => c.DataID == DataID_ProductID_Record.DataID && c.ProductID == DataID_ProductID_Record.ProductID).Distinct(); 
+5
source share
5 answers

You could do that.

 DataTestPlans.Where(c => c.DataID == YourInput && c.ProductID == YourInput) .GroupBy(x => new {x.DataID,x.TestPlanName,x.TCIndexList,x.ProductID}) .Select(x => x.First()); 
+4
source

There are two ways to do, as highlighted in this question , is not necessary for IComparer. Here is a short example that you can play with (I did not use your actual object, because it is easier to explain):

 class Program { static void Main(string[] args) { var persons = Setup(); //option 1, can stream, option suggested by Jon Skeet //https://stackoverflow.com/a/1300116/897326 var result1 = persons. DistinctBy(m => new {m.FirstName, m.LastName}); //option 2, cannot stream, but does reference to DistinctBy //https://stackoverflow.com/a/4158364/897326 var result2 = persons. GroupBy(m => new { m.FirstName, m.LastName }). Select(group => group.First()); } class Person { public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } } private static List<Person> Setup() { var p1 = new Person { FirstName = "John", LastName = "Doe", Address = "USA" }; var p2 = new Person { FirstName = "John", LastName = "Doe", Address = "Canada" }; var p3 = new Person { FirstName = "Jane", LastName = "Doe", Address = "Australia" }; var persons = new List<Person>(); persons.Add(p1); persons.Add(p2); persons.Add(p3); return persons; } } public static class LinqExtensions { public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> knownKeys = new HashSet<TKey>(); foreach (TSource element in source) { if (knownKeys.Add(keySelector(element))) { yield return element; } } } } 
+1
source

You will need to use GroupBy() , which will create an IEnumerable<IGrouping<TKey, TElement>> , which you can IEnumerable<IGrouping<TKey, TElement>> over. You can access the records either using group.First () or some aggregate function above the group.

0
source

You can use several chained chains. Make sure it is IQueryable if you are making a db call. Example below

 List<SomeClass> c = new List<SomeClass>(); var result = c.Where(x => x.ID == 4).Distinct().Where(y => y.Name == "foo").Distinct(); 
0
source

You can implement IEqualityComparer and use this default value in the Distinct() method. If you are implementing the DataTestPlansComparer example, you can use, as in the following example:

 DataTestPlans_DataID_ProductID = DataTestPlans.Where(c => c.DataID == DataID_ProductID_Record.DataID && c.ProductID == DataID_ProductID_Record.ProductID).Distinct(new DataTestPlansComparer()); 

Note that your custom mapper must be passed as a parameter to the Distinct() method.

In your case, it could be:

  public class DataTestPlanComparer : IEqualityComparer<DataTestPlan> { public bool Equals(DataTestPlan x, DataTestPlan y) { if (Object.ReferenceEquals(x, y)) return true; if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; return x.DataID == y.DataID && x.ProductID == y.ProductID; } public int GetHashCode(DataTestPlan dataTestPlan) { if (Object.ReferenceEquals(dataTestPlan, null)) return 0; int hashDataTestPlanDataID = dataTestPlan.DataID == null ? 0 : dataTestPlan.DataID.GetHashCode(); int hashDataTestPlanProductID = dataTestPlan.ProductID.GetHashCode(); return hashDataTestPlanDataID ^ hashDataTestPlanProductID; } } 

Please follow the MSDN to implement IEqualityComparer.

0
source

Source: https://habr.com/ru/post/1261746/


All Articles