linq多表查询

        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(JsonConvert.SerializeObject(new { name = "Eric", info = new { age = 20, address = "襄阳" } }));

            //linq查询语法:单表查询
            //using (StudentDBDataContext db = new StudentDBDataContext())
            //{
            //    var query = from c in db.Stu
            //                join s in db.Class
            //                on c.Cid equals s.Cid
            //                select new { c.Name, s.Cname };
            //    Response.Write(JsonConvert.SerializeObject(query));
            //}

            //linq查询语法:两表查询
            //using (StudentDBDataContext db = new StudentDBDataContext())
            //{
            //    var query = from l in db.Lesson
            //                join s in db.SCType
            //                on l.sid equals s.sid
            //                select new
            //                {
            //                    l.Lid,
            //                    l.Lname,
            //                    l.LTime,
            //                    l.Laddress,
            //                    l.LstTime,
            //                    l.LclTime,
            //                    l.Teacher,
            //                    l.limitNum,
            //                    l.actualNum,
            //                    s.TName
            //                };
            //    Response.Write(JsonConvert.SerializeObject(query));
            //}

            //linq方法语法:两表查询
            //using (StudentDBDataContext db = new StudentDBDataContext())
            //{
            //    var query = db.Lesson.Join(
            //        db.SCType,
            //        l => l.sid, s => s.sid,
            //        (l, s) => new
            //        {
            //            l.Lid,
            //            l.Lname,
            //            l.LTime,
            //            l.Laddress,
            //            l.LstTime,
            //            l.LclTime,
            //            l.Teacher,
            //            l.limitNum,
            //            l.actualNum,
            //            s.TName
            //        }).ToList();
            //    Response.Write(JsonConvert.SerializeObject(query));
            //}

            //linq方法语法:两表查询
            //using (StudentDBDataContext db = new StudentDBDataContext())
            //{
            //    var query = from c in db.Class
            //                join s in db.Stu
            //                on c.Cid equals s.Cid
            //                select new
            //                {
            //                    c.Cid,
            //                    c.Cname,
            //                    c.Ccode,
            //                    s.Name,
            //                    s.Num,
            //                    s.Pwd
            //                };
            //    Response.Write(JsonConvert.SerializeObject(query));
            //    gv.DataSource = query;
            //    gv.DataBind();
            //}

            //linq方法语法:两表查询
            //using (StudentDBDataContext db = new StudentDBDataContext())
            //{
            //    var query = db.Stu.Join(db.Class, a => a.Cid, b => b.Cid, (a, b) => new { a.Cid, a.Name, b.Cname }).ToList();
            //    Response.Write(JsonConvert.SerializeObject(query));
            //    gv.DataSource = query;
            //    gv.DataBind();
            //}

            //var query = new TestClass().QueryLesson();
            //var json = JsonConvert.SerializeObject(query);
            //Response.Write(query);
            //gv.DataSource = query;
            //gv.DataBind();

            //linq方法语法:两表查询
            //using (StuDBEntities db = new StuDBEntities())
            //{
            //    var query = db.Lesson.Join(
            //    db.SCType,
            //    l => l.sid, s => s.sid,
            //    (l, s) => new
            //    {
            //        l.Lid,
            //        l.Lname,
            //        l.LTime,
            //        l.Laddress,
            //        l.LstTime,
            //        l.LclTime,
            //        l.Teacher,
            //        l.limitNum,
            //        l.actualNum,
            //        s.TName
            //    }).ToList();
            //    Response.Write(JsonConvert.SerializeObject(query));
            //    gv.DataSource = query;
            //    gv.DataBind();
            //}

            //linq方法语法:三表查询
            //using (StuDBEntities db = new StuDBEntities())
            //{
            //    var name = "w";
            //    var query = db.Stu.Join(db.Lesson, s => s.Lid, l => l.Lid,
            //        (s, l) => new
            //        {
            //            s.Name,
            //            s.Pwd,
            //            l.Lname,
            //            l.sid
            //        }).Join(db.SCType, a => a.sid, t => t.sid,
            //        (a, t) => new
            //        {
            //            a.Name,
            //            a.Pwd,
            //            a.Lname,
            //            t.TName
            //        }).Where(a => a.Name.Contains(name)).ToList();
            //    Response.Write(JsonConvert.SerializeObject(query));
            //    gv.DataSource = query;
            //    gv.DataBind();
            //}
        }


异步 LINQ 多表查询是一种以异步方式执行的查询操作,涉及多个数据表。通常情况下,异步 LINQ 多表查询被用于处理执行时间较长的操作,以提高性能和响应性。在异步 LINQ 多表查询中,可以使用 LINQ 查询表达式或方法语法来编写查询。 具体来说,异步 LINQ 多表查询通常涉及多个数据表,通过将它们连接起来,筛选出需要的数据。这可以通过使用 `join` 关键字将两个或多个表连接在一起,使用 `where` 条件筛选数据,以及使用 `select` 投影查询结果。 举个例子,假设我们有两个表:`Customers` 和 `Orders`。我们想要查询所有姓氏为 "Smith" 的客户及其对应的订单。我们可以使用以下代码来实现异步 LINQ 多表查询: ```csharp var query = from customer in dbContext.Customers join order in dbContext.Orders on customer.Id equals order.CustomerId where customer.LastName == "Smith" select new { customer, order }; var result = await query.ToListAsync(); ``` 在上述代码中,我们使用了 LINQ 查询表达式来连接 `Customers` 和 `Orders` 表,并筛选出姓氏为 "Smith" 的客户及其对应的订单。最后,通过调用 `ToListAsync` 方法将查询结果异步返回。 总结起来,异步 LINQ 多表查询是一种以异步方式执行的查询操作,可以通过连接多个数据表,筛选数据,并投影查询结果来满足特定需求。通过使用异步 LINQ 多表查询,我们可以提高性能和响应性,特别是在处理执行时间较长的操作时。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [一文说通异步 LINQ](https://blog.csdn.net/biyusr/article/details/125575777)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Linq多表查询](https://blog.csdn.net/weixin_44543189/article/details/89303092)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值