简体   繁体   中英

How to convert my linq query of webApi2 into asynchronous code

How do i run below code snippet into another thread using async and await keywords.

var pList = (from item in (from p in db.RTLS_PERSONDTLS
                                                   where p.OPERATION_TYPE == 1
                                                   where (lsdAtc <= p.CREATED_TIME && p.CREATED_TIME <= todayEndDate)

                                                   let pPhotoRow = (from q in db.Cloud_persons_images
                                                                    where q.img_name == p.PERSON_ID
                                                                    where (lsdAtc <= q.Createdtime && q.Createdtime <= todayEndDate)
                                                                    select q).FirstOrDefault()
                                                   select new
                                                   {
                                                       p,
                                                       Img_ext = (pPhotoRow.Img_ext ?? string.Empty),
                                                       photoBytes = pPhotoRow.Person_img ?? emptyByteArray
                                                   }).ToList()

                                     select new PersonListInfoDTO
                                     {
                                         P_ID = item.p.PERSON_ID,
                                         T_ID = (int)item.p.TAG_ID.Value,
                                         PNAME = item.p.PERSONNAME,
                                         DEPT = (int)item.p.DEPARTMENT_id,
                                         DESG = (int)item.p.DESIGNATION_id,
                                         MOB_NO = item.p.MOBILE_NO,
                                         ACTINACT = (int)item.p.ACTINACT,
                                         PHOTO = new PersonPhotoInfo { PDATA = Convert.ToBase64String(item.photoBytes), PEXT = item.Img_ext },
                                         P_ZONES = (from zone in db.RTLS_TAG_ZONE_CONFIG
                                                    where (lsdAtc <= zone.created_time && zone.created_time <= todayEndDate)
                                                    where zone.tagid == item.p.TAG_ID
                                                    select (int)zone.zone_id).ToList()
                                     }).ToList();

I have tried below code snippet

 var pList = await(from item in (from p in db.RTLS_PERSONDTLS
                                                   where p.OPERATION_TYPE == 1
                                                   where (lsdAtc <= p.CREATED_TIME && p.CREATED_TIME <= todayEndDate)

                                                   let pPhotoRow = (from q in db.Cloud_persons_images
                                                                    where q.img_name == p.PERSON_ID
                                                                    where (lsdAtc <= q.Createdtime && q.Createdtime <= todayEndDate)
                                                                    select q).FirstOrDefaultAsync()
                                                   select new
                                                   {
                                                       p,
                                                       Img_ext = (pPhotoRow.Img_ext ?? string.Empty),
                                                       photoBytes = pPhotoRow.Person_img ?? emptyByteArray
                                                   }).ToListAsync()

                                     select new PersonListInfoDTO
                                     {
                                         P_ID = item.p.PERSON_ID,
                                         T_ID = (int)item.p.TAG_ID.Value,
                                         PNAME = item.p.PERSONNAME,
                                         DEPT = (int)item.p.DEPARTMENT_id,
                                         DESG = (int)item.p.DESIGNATION_id,
                                         MOB_NO = item.p.MOBILE_NO,
                                         ACTINACT = (int)item.p.ACTINACT,
                                         PHOTO = new PersonPhotoInfo { PDATA = Convert.ToBase64String(item.photoBytes), PEXT = item.Img_ext },
                                         P_ZONES = (from zone in db.RTLS_TAG_ZONE_CONFIG
                                                    where (lsdAtc <= zone.created_time && zone.created_time <= todayEndDate)
                                                    where zone.tagid == item.p.TAG_ID
                                                    select (int)zone.zone_id).ToListAsync()
                                     }).ToListAsync();

But my problem is that my linq subquery part is dependent on previous results, if previous query taking some time to execute then this query may be ambiguity. How do i overcome this problem.

I think you can use tasks in C# to help you with your problem. Here i found a link to and article which shows 7 ways to create tasks.

5 Ways to start a task

You seem to have one giant linq query expression. It would make more sense to separate it into several queries and await/async those individual queries. It would help with readability as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM