locked
Get distinct list of foreign keys with Linq RRS feed

  • Question

  • User-1556678718 posted

    Hi

    I have classes:

      public class Deal
        {
           
            public int DealId { get; set; }
            public string Title { get; set; }
            public Dealproduct[] DealProducts { get; set; }
    
        }
    
    
    
        public class Dealproduct
        {
            public int ProductId { get; set; }
        }
    
    
        public class Product
        {
            
            public int ProductID { get; set; }
            public string ProductTitle { get; set; }
           
        }

    Now I have List of deals  named selectedDeals.  and List of products is selection: 

    	selectedDeals = selectedDeals.Where(x => (something));
            List<int> deviceIds = new List<int>();
    //HERE IS THE PROBLEM. How do I put all the distinct productIds in the deviceIds list with Linq?
    selection = selection.Where(x => deviceIds.Contains(x.ProductID ));

    How do I put all the distinct productIds in the deviceIds list with Linq?

    Please advice

    Monday, November 25, 2019 12:18 PM

Answers

  • User665608656 posted

    Hi RioDD,

    According to your description, selection represents the collection of products. And what you want to do is to use linq to put all productIds after distinct in the selection into the deviceIds collection, right?

    If there is no mistake in my understanding, you can use the AddRange method to get the collection and use linq to assign it to deviceIds.

     List<int> deviceIds = new List<int>();
    
     deviceIds.AddRange(selection.Select(x => x.ProductID).Distinct().ToList());

    If my understanding is wrong, please let me know and provide your detailed requirements.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 26, 2019 7:37 AM

All replies

  • User1120430333 posted

    How do I put all the distinct productIds in the deviceIds list with Linq?

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8

    selection = selection.Where(x => deviceIds.Contains(x.ProductID )).Distinct();

    Monday, November 25, 2019 6:21 PM
  • User-1556678718 posted

    No, I don't need this, I need a way to fill deviceIds array with data

    Tuesday, November 26, 2019 7:19 AM
  • User665608656 posted

    Hi RioDD,

    According to your description, selection represents the collection of products. And what you want to do is to use linq to put all productIds after distinct in the selection into the deviceIds collection, right?

    If there is no mistake in my understanding, you can use the AddRange method to get the collection and use linq to assign it to deviceIds.

     List<int> deviceIds = new List<int>();
    
     deviceIds.AddRange(selection.Select(x => x.ProductID).Distinct().ToList());

    If my understanding is wrong, please let me know and provide your detailed requirements.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 26, 2019 7:37 AM
  • User1120430333 posted

    No, I don't need this, I need a way to fill deviceIds array with data

    The statment I showed would have made the List too without using ToList(), becuase after all, the statement never stated not to make a collection with a collection being the default behavior of a Linq query.

    Wednesday, November 27, 2019 9:50 AM
  • User1120430333 posted

    RioDD

    No, I don't need this, I need a way to fill deviceIds array with data

    The statment I showed would have made the List of distinct int(s)  holding an ID too without using AddRange() and ToList(), becuase after all, the Linq statement never stated not to make a collection with a collection being the default behavior of a Linq query.

    deviceIds = LinqQuery.Distinct();

    Wednesday, November 27, 2019 10:01 AM
  • User-1556678718 posted

    DA924

    The statment I showed would have made the List too without using ToList(), becuase after all, the statement never stated not to make a collection with a collection being the default behavior of a Linq query.

    sorry but when the Array is empty it will not do nothing. No DA924 this is not the solution. The solution is with AddRange

    Friday, November 29, 2019 6:00 AM
  • User1120430333 posted

    RioDD

    DA924

    The statment I showed would have made the List too without using ToList(), becuase after all, the statement never stated not to make a collection with a collection being the default behavior of a Linq query.

    sorry but when the Array is empty it will not do nothing. No DA924 this is not the solution. The solution is with AddRange

    You should be checking the Array.Count > 0 before you do anything. That's a known thing in working with an array that developers do all the time. Why would you waste cpu cycles trying to do something with an empty array,  even if using an Addrange()?

    Your reply is questionable. Sorry, you do not know what you are talking about.

    Thursday, December 5, 2019 1:25 PM