none
HashSet -LinQ - Query - Select in a new object RRS feed

  • Question

  • Hello,
    I have to create a query that uniquely filters column 4. How can I achieve this?
    my list
    : 1, 1, 1, 1, True, 0, 0
    : 1, 2, 2, 2, True, 0, 0
    : 1, 3, 3, 3, True, 1, 0
    : 1, 4, 4, 4, True, 0, 0
    : 1, 5, 5, 4, True, 1, 0
    : 2, 1, 1, 1, True, 0, 0
    : 2, 2, 2, 2, True, 0, 0
    : 2, 3, 3, 3, True, 1, 0
    : 2, 4, 4, 4, True, 0, 0
    : 2, 5, 5, 5, True, 1, 0
    : 2, 6, 6, 6, True, 0, 0
    : 2, 7, 7, 7, True, 0, 0
    
    my target
     1, True
     2, True
     4, True
     6, True
     7, True
    my questions
    Is it possible to make this with a LinQ query to make a new list with two colums?
    If yes, how is the best way?
    We have 2 spellings.
       *  the short with    =>
       *  with from where   .....
    What about both?
    var resultsUniqueCounterIndices = hashsetProduct.Where(r => r.Item5 == true && r.Item6 == 0 && r.Item7 == 0)
                                    .GroupBy(x => x.Item4)
                                    .Select(g => g.FirstOrDefault()).ToList();
    
    var resultsUniqueCounterIndices2 = hashsetProduct.Where(r => r.Item5 == true && r.Item6 == 0 && r.Item7 == 0)
    				   .GroupBy(x => x.Item4).ToList();
    //.Select(g => g.FirstOrDefault()).ToList();
    
    var query = from m in resultsUniqueCounterIndices
    			where m.Item5 == true && m.Item6 == 0 && m.Item7 == 0
    			group m by m.Item4 into g
    			select new { Name = g.Key, KeyCols = g.ToList() };
    foreach (var itemTest in query)
    {
    	foreach (var inner in itemTest.KeyCols)
    	{
    		Trace.Writeline($"{inner.Item1}, {inner.Item2}, {inner.Item3}, {inner.Item4}, {inner.Item5}, {inner.Item6}, {inner.Item7}");
    	}
    }

    With best regards Markus
    Friday, February 23, 2018 3:10 PM

Answers

  • >>Can you give me  please the solution for another possibilty? 

    Sorry for delay reply. What do you mean "another possibility". It would be clearly if you give me input and expected output.

     The input of hashsetProduct as below.

    The code.

      var result = hashsetProduct.Where(r => r.Item5 == true && r.Item6 == 0 && r.Item7 == 0).Select(x => new MyResult{ Result1=x.Item4.ToString(), Result2=x.Item5.ToString() }).Distinct();

    The result.

    Best regards,

    Neil Hu


    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.

    • Marked as answer by Markus Freitag Monday, February 26, 2018 5:26 PM
    Monday, February 26, 2018 10:16 AM
    Moderator

All replies

  • Hello Markus,

    Try the below query directly, The distinct method remove the same elements.

      var result = hashsetProduct.Where(r => r.Item5 == true && r.Item6 == 0 && r.Item7 == 0).Select(x => new { x.Item4, x.Item5 }).Distinct();

    The result

    Best regards,

    Neil Hu


    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.

    Saturday, February 24, 2018 6:07 AM
    Moderator
  • Hello Fei Hu,

    thanks.

    var result = hashsetProduct.Where(r => r.Item5 == true && r.Item6 == 0 &&

    r.Item7 == 0).Select(x => new { x.Item4, x.Item5 }).Distinct();


    var query = from m in resultsUniqueCounterIndices
    			where m.Item5 == true && m.Item6 == 0 && m.Item7 == 0
    			select new {  x.Item4, x.Item5 };

    Can you give me  please the solution for another possibilty?  

       new that is it.

    If my new object is a class, like

    class MyResult

    {    string Result1;

         string Result2

    }

    x => new MyResult { x.Item4, x.Item5 }
    x => new MyResult { Result1=x.Item4, Result2=x.Item5 }

    Maybe you can give me some examples.

      Thanks in advance.

    Regards Markus

    Saturday, February 24, 2018 11:59 AM
  • >>Can you give me  please the solution for another possibilty? 

    Sorry for delay reply. What do you mean "another possibility". It would be clearly if you give me input and expected output.

     The input of hashsetProduct as below.

    The code.

      var result = hashsetProduct.Where(r => r.Item5 == true && r.Item6 == 0 && r.Item7 == 0).Select(x => new MyResult{ Result1=x.Item4.ToString(), Result2=x.Item5.ToString() }).Distinct();

    The result.

    Best regards,

    Neil Hu


    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.

    • Marked as answer by Markus Freitag Monday, February 26, 2018 5:26 PM
    Monday, February 26, 2018 10:16 AM
    Moderator
  • Hello Fei Hu,

    perfect, that is it! Thank you very much.
    If possible, when would I need to take a hashtable and we would look it there?
        Advantages and disadvantages?

    The initial question answered perfectly.

    Greeting Markus

     
    Monday, February 26, 2018 5:29 PM