linq查询中的竞速条件

时间:2017-07-29 15:15:16

标签: c# linq sqlite async-await

我的竞争情况下面的项目查询。 select miIds.ItemId不等待allItemList数据库调用完成返回所有结果。解决问题的最佳方法是什么?我应该从查询中拉出查询并等待它返回,然后开始查询,还是我可以在查询中做些什么让它等待? 谢谢。

    private async Task LoadData()
    {
        // Query Mission_Item Ids.
        var missionItemsList = await App.Database.GetTableAsync<Mission_Items_Model>();
        var missionItemIds = from missionItem in missionItemsList
                             where missionItem.MissionId == game.CurrentMissionId
                             select missionItem;

        // Query All Items, select item where item's itemIds match mission's itemIds.
        var allItemList = await App.Database.GetTableAsync<ItemModel>();
        var items = from item in allItemList
                    let mi = (from miIds in missionItemIds where miIds.ItemId == item.Id select miIds.ItemId)
                    where mi.Contains(item.Id)
                    select item;

        // Item list for mission.
        ItemsList = new List<ItemModel>(items);

        ...

    }

1 个答案:

答案 0 :(得分:1)

您的问题很简单,因使用var而导致查询missionItemIds未被枚举。要使该查询实际执行,您需要使用一个实现序列的调用(例如.ToList()):

var missionItemIds = (from missionItem in missionItemsList
                     where missionItem.MissionId == game.CurrentMissionId
                     select missionItem).ToList();

game.CurrentMissionId似乎在您实际实现结果时发生了变化。