How to translate my LINQ code to sql query RRS feed

  • Question

  • How to translate below LINQ to sql. in this LINQ i am joining a datatable & linq. here i will return those data from datatable dtFilter10QK 
    whose section & lineitem does not exist in list lstMapData. tell me how to write same equivalent sql.
    DataTable dtJoinedData = (from _10qk in dtFilter10QK.AsEnumerable()
                                                join mappedlst in lstMapData
                                                on new 
                                                    Section = _10qk.Field<string>("Section"),
                                                    LineItem = _10qk.Field<string>("Line Item")
                                                equals new 
                                                    Section = mappedlst.Section,
                                                    LineItem = mappedlst.LineItem
                                                into g
                                                where g.Count() == 0
                                                select _10qk).CopyToDataTable();

    Saturday, September 14, 2019 5:53 AM

All replies

  • Hello,

    Best recommendation is to take time to learn how to write the query without even looking at the current code. A rule of thumb is when writing LINQ against database data is to first understand how to write plain old SQL then model LINQ from the SQL.

    Learn about SQL various JOIN and GROUP BY clauses goes a long way to understanding a conversion.

    If using SQL-Server write the query directly in Visual Studio from Server Explorer or even better, use SSMS (SQL-Server Management Studio).

    With SSMS (first create the relations between your tables under the database node "Database diagrams) you select the main table then highlight the query, right click, select design query in editor.

    Here is an example of using the designer to create a fairly complex query.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Saturday, September 14, 2019 10:24 AM