none
entity framework Group by multiple columns and count the number of groups with more than 1 item RRS feed

  • Question

  • Hello   !

    I'm using Entity Framework 6

    I have Myobject1 : Name , City , Price Value

    I want to group all the items on that table on groups that have the same Name , city and Price.

    After I want to count the groups that have more than 1 item.

    I'm using this code :

    Dim q As Integer = context.myobjects.GroupBy(Function(x) New With {x.Name,x.City,x.Price}).Where(Function(t3) t3.Count > 1).Count

    But this query always return 0

    But on my table I have several groups that have more than 1 item.

    What is wrong with my query ?

    Thank you !

    Thursday, December 6, 2018 9:43 PM

All replies

  • Hi dcode25,

    Based on your description and related code, I create a simple demo as below, it works well, could you please share related records in database.

    #Code.

        Sub Main()
    
            Using context As New Model1
    
                Dim q As Integer = context.myobjects.GroupBy(Function(x) New With {x.Name, x.City, x.Price}).Where(Function(t3) t3.Count > 1).Count
    
                Console.ReadLine()
    
            End Using
    
        End Sub

    #Table

    CREATE TABLE [dbo].[myobjects] (
        [Id]    INT          NOT NULL,
        [Name]  VARCHAR (50) NOT NULL,
        [City]  VARCHAR (50) NOT NULL,
        [Price] INT          NOT NULL
    );

    #Records

    #Result

    Best regards,

    Zhanglong


    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.

    Friday, December 7, 2018 3:30 AM
    Moderator