Home > LINQ > Joins in LINQ to SQL

Joins in LINQ to SQL

//Query Sample
                var query = from table in db.Activities
                                                    select new
                                                    {
                                                        table.AID,
                                                        table.AddBy,
                                                        table.TotalAmount,
                                                        table.DateofStart,
                                                        table.Details,
                                                        table.DateOfPlan
                                                    };
                //Inner join Sample
                var query = from dataA in db.Activities join dataAD in db.ActivityDetails on dataA.AID equals dataAD.AID
                                                    select new
                                                    {
                                                        dataA.AID,
                                                        dataA.AddBy,
                                                        dataA.TotalAmount,
                                                        dataA.DateofStart,
                                                        dataA.Details,
                                                        dataA.DateOfPlan,
                                                        dataAD.SID,
                                                        dataAD.DateofPay
                                                    };
                //left outer join Sample
                var query = from dataA in db.Activities
                            join dataAD in db.ActivityDetails on dataA.AID equals dataAD.AID into temp
                            from x in temp.DefaultIfEmpty()
                            select new
                            {
                                dataA.AID,
                                dataA.AddBy,
                                dataA.TotalAmount,
                                dataA.DateofStart,
                                dataA.Details,
                                dataA.DateOfPlan,
                                //x.SID,
                                x.Amount,
                                x.DateofPay
                            };

Advertisements
Categories: LINQ
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: