如何将此 linq 转换为 bool
How can I convert this linq to bool
如何将查询转换为布尔值?
我使用了 "ALL (x => x)" 但没有给出我需要的答案。
代码行
checkItemInventory.Where(x => listCost.Contains(x.Id));
在这种情况下,listcost
会有 2
项,我需要检查 checkItemInventory
是否有这些 2
项。
“库存中的所有物品都有一个显示在 listcost 中的 ID”。 listCost 需要具有与 inventory 相同数量的项目(假设 Id 是唯一的)可能更多,以便有机会返回 true
checkItemInventory.All(x => listCost.Contains(x.Id))
“库存中至少有一项的 ID 也在 listCost 中”。 Listcost 最少只能有一个 id,才有机会返回 true
checkItemInventory.Any(x => listCost.Contains(x.Id))
如您所见,这些都不是您想要的,因为您似乎在说您想检查 listcost 中的每个项目是否也存在于库存中。这类似于顶部代码,但反过来(“listCost 中的所有项目都存在于库存中”与“库存中的所有项目都存在于 listcost 中”
我想我会先从库存中制作一本字典,除非它已经支持快速查找:
var d = checkItemInventory.Select(x => new { x.Id, x.Id }).ToDictionary();
var boolResult = listCost.All(lc => d.ContainsKey(lc));
如果库存较小,您可以使用这种方法:
listCost.All(lc => checkItemInventory.Any(cii => cii.Id == lc));
请注意,它在内部可能会执行以下操作:
bool all = true;
foreach(lc in listCost){
bool found = false;
foreach(cci in checkItemInventory)
if(lc == cci.Id){
found = true;
break;
}
all &= found;
if(!all)
return false;
}
return true;
其中有很多重复比较(对于 listCost 中的每个项目,扫描整个库存),可能会很慢
编辑
我要求澄清您如何存储库存以及构建项目的成本。这是我做出的一个假设,以及基于它的解决方案如何工作:
假设你的物品栏有物品的种类和玩家携带物品的数量:
class InventoryItem{
int ItemKindId { get; set;}
int CountOf { get; set; }
}
player.Inventory.Add(new InventoryItem() {
ItemKindId = Constants.WOOD, //1
CountOf = 10 //holding 10 items of wood
};
player.Inventory.Add(new InventoryItem() {
ItemKindId = Constants.STONE, //2
CountOf = 5 //holding 5 items of stone
};
假设您有一个制作食谱,例如一把斧头,它需要 1 个木头和 2 个石头,但它以简单的顺序列出它们:
int[] axeRecipe = new int[] { Constants.WOOD, Constants.STONE, Constants.STONE };
可能最容易对食谱进行分组:
var recipe = axeRecipe.GroupBy(item => item)
/*
now we have a grouping of the recipe[item].Key as the material and a
recipe[item].Count() of how much. The group is like a dictionary:
recipe[Constants.WOOD] = new List<int>{ Constants.WOOD };
recipe[Constants.STONE] = new List<int>{ Constants.STONE, Constants.STONE, };
A group item has a Key and a list of objects that have that key
Because my recipe was simply ints, the Key is the same number as all the
items in the list
*/
//for all items in the recipe
grp.All(groupItem =>
//does the player inventory contain any item
playerInventory.Any(inventoryItem =>
//where the material kind is the same as the recipe key (material)
inventoryItem.ItemKindId == groupItem.Key &&
//and the count they have of it, is enough to make the recipe
inventoryItem.CountOf >= groupItem.Count()
);
如果需要,您当然可以将其缩减为一行:axeRecipe.GroupBy(...).All(...)
您可以将 listCost
映射到 int
的列表,然后使用 Except()
和 Any()
检查是否包含所有项目:
bool containsAll = !listCost.Select(x => x.Id).Except(checkItemInventory).Any();
[更新]
您告诉我们以下内容:
How to convert a query to bool? I used the "ALL (x => x)" but did not give the answer I needed.
checkItemInventory.Where(x => listCost.Contains(x.Id));
In this case, the listcost would have 2 items, I needed to check if
the checkItemInventory has these 2 items.
如果你需要检查是否有任何结果,那么你可以使用:
bool hasItems = checkItemInventory.Where(x => listCost.Contains(x.Id)).Any();
如果需要计算结果可以使用
checkItemInventory.Where(x => listCost.Contains(x.Id)).Count();
您可以使用 Join
创建基于 Linq 查询的方法,并使用结果检查列表的长度是否大于 0。然后将其转换为布尔值。
var query = checkItemInventory.Join(listCost,
inventory => inventory.Id,
cost => cost.Id,
(inventory, cost) => new { id = inventory.Id });
var count = query.ToList().Count();
var b = (count > 0);
如果我理解正确,listCost
可以比 checkItemInventory
少 个元素。您要检查 listCost 中的所有元素是否在 checkItemInventory 中都有对应的元素。正确的?如果是,试试这个:
listCost.All(x => checkItemInventory.Contains(x));
我不知道这些列表的类型,所以你可能需要在某些地方使用x.id
如何将查询转换为布尔值? 我使用了 "ALL (x => x)" 但没有给出我需要的答案。
代码行
checkItemInventory.Where(x => listCost.Contains(x.Id));
在这种情况下,listcost
会有 2
项,我需要检查 checkItemInventory
是否有这些 2
项。
“库存中的所有物品都有一个显示在 listcost 中的 ID”。 listCost 需要具有与 inventory 相同数量的项目(假设 Id 是唯一的)可能更多,以便有机会返回 true
checkItemInventory.All(x => listCost.Contains(x.Id))
“库存中至少有一项的 ID 也在 listCost 中”。 Listcost 最少只能有一个 id,才有机会返回 true
checkItemInventory.Any(x => listCost.Contains(x.Id))
如您所见,这些都不是您想要的,因为您似乎在说您想检查 listcost 中的每个项目是否也存在于库存中。这类似于顶部代码,但反过来(“listCost 中的所有项目都存在于库存中”与“库存中的所有项目都存在于 listcost 中”
我想我会先从库存中制作一本字典,除非它已经支持快速查找:
var d = checkItemInventory.Select(x => new { x.Id, x.Id }).ToDictionary();
var boolResult = listCost.All(lc => d.ContainsKey(lc));
如果库存较小,您可以使用这种方法:
listCost.All(lc => checkItemInventory.Any(cii => cii.Id == lc));
请注意,它在内部可能会执行以下操作:
bool all = true;
foreach(lc in listCost){
bool found = false;
foreach(cci in checkItemInventory)
if(lc == cci.Id){
found = true;
break;
}
all &= found;
if(!all)
return false;
}
return true;
其中有很多重复比较(对于 listCost 中的每个项目,扫描整个库存),可能会很慢
编辑
我要求澄清您如何存储库存以及构建项目的成本。这是我做出的一个假设,以及基于它的解决方案如何工作:
假设你的物品栏有物品的种类和玩家携带物品的数量:
class InventoryItem{
int ItemKindId { get; set;}
int CountOf { get; set; }
}
player.Inventory.Add(new InventoryItem() {
ItemKindId = Constants.WOOD, //1
CountOf = 10 //holding 10 items of wood
};
player.Inventory.Add(new InventoryItem() {
ItemKindId = Constants.STONE, //2
CountOf = 5 //holding 5 items of stone
};
假设您有一个制作食谱,例如一把斧头,它需要 1 个木头和 2 个石头,但它以简单的顺序列出它们:
int[] axeRecipe = new int[] { Constants.WOOD, Constants.STONE, Constants.STONE };
可能最容易对食谱进行分组:
var recipe = axeRecipe.GroupBy(item => item)
/*
now we have a grouping of the recipe[item].Key as the material and a
recipe[item].Count() of how much. The group is like a dictionary:
recipe[Constants.WOOD] = new List<int>{ Constants.WOOD };
recipe[Constants.STONE] = new List<int>{ Constants.STONE, Constants.STONE, };
A group item has a Key and a list of objects that have that key
Because my recipe was simply ints, the Key is the same number as all the
items in the list
*/
//for all items in the recipe
grp.All(groupItem =>
//does the player inventory contain any item
playerInventory.Any(inventoryItem =>
//where the material kind is the same as the recipe key (material)
inventoryItem.ItemKindId == groupItem.Key &&
//and the count they have of it, is enough to make the recipe
inventoryItem.CountOf >= groupItem.Count()
);
如果需要,您当然可以将其缩减为一行:axeRecipe.GroupBy(...).All(...)
您可以将 listCost
映射到 int
的列表,然后使用 Except()
和 Any()
检查是否包含所有项目:
bool containsAll = !listCost.Select(x => x.Id).Except(checkItemInventory).Any();
[更新]
您告诉我们以下内容:
How to convert a query to bool? I used the "ALL (x => x)" but did not give the answer I needed.
checkItemInventory.Where(x => listCost.Contains(x.Id));
In this case, the listcost would have 2 items, I needed to check if the checkItemInventory has these 2 items.
如果你需要检查是否有任何结果,那么你可以使用:
bool hasItems = checkItemInventory.Where(x => listCost.Contains(x.Id)).Any();
如果需要计算结果可以使用
checkItemInventory.Where(x => listCost.Contains(x.Id)).Count();
您可以使用 Join
创建基于 Linq 查询的方法,并使用结果检查列表的长度是否大于 0。然后将其转换为布尔值。
var query = checkItemInventory.Join(listCost,
inventory => inventory.Id,
cost => cost.Id,
(inventory, cost) => new { id = inventory.Id });
var count = query.ToList().Count();
var b = (count > 0);
如果我理解正确,listCost
可以比 checkItemInventory
少 个元素。您要检查 listCost 中的所有元素是否在 checkItemInventory 中都有对应的元素。正确的?如果是,试试这个:
listCost.All(x => checkItemInventory.Contains(x));
我不知道这些列表的类型,所以你可能需要在某些地方使用x.id