使用Linq查询的大量数据的超时异常

时间:2017-02-10 10:58:14

标签: c# asp.net linq

我的Linq查询仅适用于开发数据库中的少数数据但相同的查询在生产数据库中给出了超时异常。在生产数据库中有很多数据。我不想增加命令时间,而是想要一个优化的查询。任何人都可以帮我优化这个查询吗?

var poStatus = (from s in ThisYearDataOnly
                                    join st in status on s.StatusID equals st

                                    group s by s.MRIPatientOrderId
                                        into x
                                    select
                                        new
                                        {
                                            MRIPatientOrderId = x,
                                            StatusId = x.OrderByDescending(c => c.ID).FirstOrDefault().StatusID
                                        }).ToList();

要了解提供先前查询的数据:

sharedmsdbEntity sdb = new sharedmsdbEntity();
                using (MRIdbEntity db = new MRIdbEntity())
                {
                    string type = Request.QueryString["type"];
                    int typeID = int.Parse(Request.QueryString["typeID"]);


                    List<string> statusList = new List<string>() { "Complete" };
                    var completeStatusId = (from s in sdb.smsStatus
                                            where s.IsMRI && s.Status == MRIStatusEnum.Complete.ToString()
                                            select s.ID).FirstOrDefault();



                    var status = (from s in sdb.smsStatus select s.ID).ToList();

                    var PoOrdersYearly = (from po in db.smsMRIPatientOrders
                                          where
                                         ((po.ExamDateIn.HasValue && (po.ExamDateIn.Value.Year == DateTime.Now.Year)) || (!po.ExamDateIn.HasValue && po.ExamDateIn.HasValue && (po.ExamDateIn.Value.Year == DateTime.Now.Year))) 
                                          select po);

                    var ThisYearDataOnly = (from s in db.smsMRIPatientOrderStatus join po in PoOrdersYearly on s.MRIPatientOrderId equals po.ID select s );

0 个答案:

没有答案