Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

Linq: Query data with a left join

 

 

Example of a left join query in Linq

//--< Get Linq.Query >--

//*gets last 10 Notes with View_Sum

var notes_Data =  (from n in _dbContext.tbl_Notes

       join s in _dbContext.tbl_Notes_View_Sums on n.IDNote equals s.IDView into sJoined

       orderby n.IDNote descending

       from sLeft in sJoined.DefaultIfEmpty()

       select new { n, sumX=sLeft.Sum_Views }).Take(10);  

 

//--</ Get Linq.Query >--

 

A left join query is made when two tables are joined in the SQL database, with one side of a table not having data associated with each entry in the second table.

Example:

Here are 3 notes IDView = 1,2,3 associated with a totals info file. In the summary info file, however, there is only one single entry as an endorsement with the IDView = 1

 

The data is then output to a display view in Asp.Net Core MVC

 

Complete code example

// GET: /Note Root

        public async Task<IActionResult> Index()

        {

            //-------------< Index /Notes >-------------

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDUser = await Userinfos.UserInfo_Methods.getIDUser_as_Number(this.User,_dbContext ) ;

            //--</ Get User ID >--

 

            //< content_logger >

            int? sumLogViews= await logViewCounter_erhoehen(IDUser);

            //</ content_logger >

 

 

            //*fill a Linq.Table

            //< Linq: Table.Rows >

            List<NoteModel> notes = await (from n in _dbContext.tbl_Notes where n.IDUser == IDUser orderby n.IDNote descending select n).Take(10).ToListAsync() ;

            //</ Linq: Table.Rows >

 

            //--< Get Linq.Query >--

            //*gets last 10 Notes with View_Sum

            var notes_Data =  (from n in _dbContext.tbl_Notes

                               join s in _dbContext.tbl_Notes_View_Sums on n.IDNote equals s.IDView into sJoined

                               orderby n.IDNote descending

                               from sLeft in sJoined.DefaultIfEmpty()

                               select new { n, sumX=sLeft.Sum_Views }).Take(10);  

 

            //--</ Get Linq.Query >--

 

            //--< fill Data_to_View >--

            List<Notes_Index_DataModel> data_to_View = new List<Notes_Index_DataModel>();

            foreach (var note_data in notes_Data)

            {

               //< Data >

                Notes_Index_DataModel data = new Notes_Index_DataModel();

                data.IDUser = IDUser;

                data.Sum_Views = note_data.sumX ;

                data.Note = note_data.n ;               

                //</ Data >

 

                //< add >

                data_to_View.Add(data);

                //</ add >

            }

            //--</ fill Data_to_View >--

 

 

            //< out to view >

            return View(data_to_View);

            //</ out to view >

            //-------------</ Index /Notes >-------------

        }

 

 

 

Mobile
»
Linq: Output to a variable list
»
Linq: Select Fields in Query
»
the type or namespace name dbAppData_Context does not exist
»
Delete record in Linq
»
Code snippet: Create Linq Select table from ID number fields
»
EF: Determine new ID of a data record
»
Asp MVC: Output Data from SQL Server directly to a List Type
»
Linq: Query data with a left join
»
Asp Linq: Collect data with a LINQ query from SQL Server

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de