Return only a single row from duplicate rows with MySQL

Oneil Drummond 40 Reputation points
2023-03-03T18:39:57.3+00:00
I have an ASP.NET Core Web App (MVC) 3.1 project using Entity Framework Core to generate a model for a SQL database using Nuget Console.

When data is pulled into jQuery. Datatable shows a duplicate result because of the extension. I only need the 4 digi ext. rows.

FirstName  |  LastName | Email            | Title     |Number 
---------------------------------------------------------
John       |  Doe     | ******@exmaple.com  | Accountant| 6543

John       |  Doe     | ******@exmaple.com  | Accountant| 76543

How can I eliminate the row with the 5 digi ext. 


 using (newMA4000Context _context = new newMA4000Context())
                {

                    List<UserExtensionVw> userExtensionVws1 = _context.UserExtensionVws.ToList();
                    List<UserVw> userExtensionVws2 = _context.UserVws.ToList();

              
                    var TableJoin = from e in userExtensionVws1
                                    join d in userExtensionVws2 on e.Userid equals d.Userid into table
                                    from d in table.ToList()

                                    

                                    select new MA400_ViewModel
                                    {

                                        GetUserExtensionVw = e,
                                        GetUserVw = d


                                    };


                

                return View(TableJoin.ToList());
                }
Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. Anonymous
    2023-03-09T07:08:02.39+00:00

    Hi @Oneil Drummond

    List<UserExtensionVw> userExtensionVws1 = _context.UserExtensionVws.ToList();

    From your query statement and result data, I assume the userExtensionVws1 might contains the duplicate UserExtensionVw, which has the same Userid with different Number, right? like this:

    User's image

    Then, if we directly use the query statement, it will get your current result.

    To get your required result, before joining the table, you can group the userExtensionVws1 and sort each group by the number property, then get the first item of each group. Code like this:

        List<UserExtensionVw> newuserExtensionVws = userExtensionVws1.GroupBy(c => c.Userid).Select(g => g.OrderBy(p => p.Number).First()).ToList();
    
    

    After that use the filter result to join the tables.

                List<UserExtensionVw> newuserExtensionVws = userExtensionVws1.GroupBy(c => c.Userid).Select(g => g.OrderBy(p => p.Number).First()).ToList();
    
                var TableJoin = from e in newuserExtensionVws 
                                 join d in userExtensionVws2 on e.Userid equals d.Userid into table
                                from d in table.ToList()  
                                select new MA400_ViewModel
                                { 
                                    GetUserExtensionVw = e,
                                    GetUserVw = d 
                                };
                 
                return View(TableJoin.ToList());
    

    The result as below:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-03-03T22:05:00.8733333+00:00

    Create view with only the columns you want and use a GROUP BY or DISTINCT. See the MySQL doc for how to create a View. See the EF docs for calling a View.

    Keyless Entity Types

    Another option is create a raw query.

    SQL Queries

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.