简体   繁体   中英

How to call a local method from the linq query

In my web api i need to execute a method from linq query itself. My linq query code snippet belongs to method is shown below which calls local method to get required data.

var onlineData = (from od in db.RTLS_ONLINEPERSONSTATUS
                  let zoneIds = db.RTLS_PERSONSTATUS_HISTORY.Where(p => p.person_id == od.PERSONID).OrderByDescending(z => z.stime > startOfThisDay && z.stime < DateTime.Now).Select(z => z.zone_id).ToList()

                  let zoneIdsArray = this.getZoneList((zoneIds.ToArray()))
                  let fzones = zoneIdsArray.Select(z => z).Take(5)
                  select new OnlineDataInfoDTO
                  {
                      P_ID = od.PERSONID,
                      T_ID = (int)od.TAGID,
                      LOCS = fzones.ToList()
                  }

public int[] getZoneList(decimal[] zoneIdsArray)
    {
        int[] zoneIds = Array.ConvertAll(zoneIdsArray, x => (int)x);
        List<int> list = zoneIds.ToList();
        for (int c = 1; c < zoneIdsArray.Count(); c++)
        {
            if (zoneIdsArray[c] == zoneIdsArray[c - 1])
            {
                list.Remove((int)zoneIdsArray[c]);
            }
        }
        return list.ToArray();
    }

I am getting exception at let zoneIdsArray = this.getZoneList((zoneIds.ToArray())), is there any way to solve this problem. I got logic to solve my problem from this link( Linq query to get person visited zones of current day ), the given logic is absolutely fine for my requirement but i am facing problem while executing it.

One way to achieve that would be to perform the projection on the client instead of the underlying LINQ provider. This can be done by separating your query into 2 steps:

var peopleStatus =
    from od in db.RTLS_ONLINEPERSONSTATUS
    let zoneIds = db.RTLS_PERSONSTATUS_HISTORY
        .Where(p => p.person_id == od.PERSONID)
        .OrderByDescending(z => z.stime > startOfThisDay && z.stime < DateTime.Now)
        .Select(z => z.zone_id)
        .ToList()
    select new
    {
        Person = od,
        ZoneIds = zoneIds,
    };

var onlineData =
    from od in peopleStatus.ToList()
    let zoneIdsArray = this.getZoneList((od.ZoneIds.ToArray()))
    let fzones = zoneIdsArray.Select(z => z).Take(5)
    select new OnlineDataInfoDTO
    {
        P_ID = od.Person.PERSONID,
        T_ID = (int)od.Person.TAGID,
        LOCS = fzones.ToList()
    };

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