locked
HashSet with an object RRS feed

  • Question

  • Hello,
    can I use a hashset with an object?
    See below. Is that unique?
    HashSet<Tuple<int, string, INFO>> HashsetINFOs;
    1, "100001", KW=2,EnergyClass=A
    2, "100002", KW=2,EnergyClass=A
    3, "100003", KW=3,EnergyClass=B
    4, "100004", KW=2,EnergyClass=A
    Or I must use a dictionary.

    With best regards Markus
    public class INFO
    {
    	public string KW;
    	public string EnergyClass; 
    }
    			
    // Index, partnumber, INFO
    Dictionary<Tuple<int, string>, INFO> DicDevice;
    //public HashSet<Tuple<int, string, INFO>> HashsetINFOs;
    
    public Order()
    {
    	Program = "";
    
    	//  HashsetINFOs = new HashSet<Tuple<int, string, INFO>>();
    	DicDevice = new Dictionary<Tuple<int, string>, INFO>();
    }


    Monday, July 16, 2018 4:59 PM

Answers

  • Your example values are unique and it depends on your business requirements needs that either you need Dictionary or HashSet.

    You need to decide as per your particular scenario needs. If your intent is to have Key and Value then use Dictionary otherwise you can use HashSet. Dictionary would throw exception while adding duplicate key while HashSet won't add element with same value again but in dictionary you would need to explicitly check that the key does not already exists.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Marked as answer by Markus Freitag Saturday, July 28, 2018 10:59 AM
    • Edited by Ehsan Sajjad Saturday, July 28, 2018 7:29 PM typo correction
    Monday, July 16, 2018 5:55 PM
  • Hi Markus Freitag,

    Thank you for posting here.

    You could try to use Anonymous Type. Here is the simple code for your reference.

      string[] source = new string[] { "1", "100001", "2", "A", "2", "100002", "2", "A", "3", "100003", "3", "B", "4", "100004", "2", "A" };
                var result = source
                    .Select((element, index) => new { element, index })
                    .GroupBy(x => x.index / 4)
                    .Select(x => 
                    new
                    {
                        ID = x.ElementAt(0).element,
                        Number = x.ElementAt(1).element,
                        KW = x.ElementAt(2).element,
                        EnergyClass = x.ElementAt(3).element,
    
                    }).ToList();


    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 17, 2018 7:05 AM

All replies

  • Your example values are unique and it depends on your business requirements needs that either you need Dictionary or HashSet.

    You need to decide as per your particular scenario needs. If your intent is to have Key and Value then use Dictionary otherwise you can use HashSet. Dictionary would throw exception while adding duplicate key while HashSet won't add element with same value again but in dictionary you would need to explicitly check that the key does not already exists.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Marked as answer by Markus Freitag Saturday, July 28, 2018 10:59 AM
    • Edited by Ehsan Sajjad Saturday, July 28, 2018 7:29 PM typo correction
    Monday, July 16, 2018 5:55 PM
  • Hi Markus Freitag,

    Thank you for posting here.

    You could try to use Anonymous Type. Here is the simple code for your reference.

      string[] source = new string[] { "1", "100001", "2", "A", "2", "100002", "2", "A", "3", "100003", "3", "B", "4", "100004", "2", "A" };
                var result = source
                    .Select((element, index) => new { element, index })
                    .GroupBy(x => x.index / 4)
                    .Select(x => 
                    new
                    {
                        ID = x.ElementAt(0).element,
                        Number = x.ElementAt(1).element,
                        KW = x.ElementAt(2).element,
                        EnergyClass = x.ElementAt(3).element,
    
                    }).ToList();


    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 17, 2018 7:05 AM