How to call a local method from the linq query

岁酱吖の 提交于 2019-12-12 04:25:33

问题


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.


回答1:


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()
    };


来源:https://stackoverflow.com/questions/43188596/how-to-call-a-local-method-from-the-linq-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!