Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则数组Linq_C#_Arrays_Linq - Fatal编程技术网

C# 正则数组Linq

C# 正则数组Linq,c#,arrays,linq,C#,Arrays,Linq,我有一个Item数组(我创建了一个类),这就是我尝试做的: foreach (Recipe recipe in recipes) { if (recipe.All(i => { Item item = inventory.Inventory.FirstOrDefault(x => x.ID == i.ID); return (item == null) ? false : (item.Amount >= i.Amount);

我有一个Item数组(我创建了一个类),这就是我尝试做的:

foreach (Recipe recipe in recipes)
{
    if (recipe.All(i =>
    {
        Item item = inventory.Inventory.FirstOrDefault(x => x.ID == i.ID);
        return (item == null) ? false : (item.Amount >= i.Amount);
    }))
        ableToCraft.Add(recipe);
}
谢谢你的提问

问题是,当我用x(x=>x.ID==I.ID)遍历清单时,我遇到了x begin null,因为x试图从数组指向的单元格也是null

当程序在数组中遇到空单元格时,如何解决该问题并使程序跳过?

试试看

(Recipe recipe in recipes)
                {
                    if (recipe.All(i =>
                    {
                        // this
                        Item item = inventory.Inventory.FirstOrDefault(x => x != null && x.ID == i.ID);
                        return (item == null) ? false : (item.Amount >= i.Amount);
                    }))
                        ableToCraft.Add(recipe);
                }
试一试

您可以先检查null:

或筛选第一个或默认值之前的记录:

inventory.Inventory
.Where(x => x != null)
.FirstOrDefault(x => x.ID == i.ID);
您可以先检查null:

或筛选第一个或默认值之前的记录:

inventory.Inventory
.Where(x => x != null)
.FirstOrDefault(x => x.ID == i.ID);

将所有内容捆绑到
foreach
中有点。。。令人困惑

对于维护,我会使用
Func
使其更易于维护

Func<Recipe, bool> pricesAreCorrectForItem = (recipe) =>
     {
          var validInventoryItems = inventory.Inventory.Where(x=>x!=null);
          var item = validInventoryItems.FirstOrDefault(x=>x.ID == recipe.ID);
          return item == null ? false : (item.Amount >= recipe.Amount);
     };

foreach(Recipe recipe in recipes)
{
    if(recipe.All(pricesAreCorrectForItem))
        ableToCraft.Add(recipe);
}
Func pricesarecocorrectforitem=(配方)=>
{
var validInventoryItems=inventory.inventory.Where(x=>x!=null);
var item=validInventoryItems.FirstOrDefault(x=>x.ID==recipe.ID);
返回项==null?false:(item.Amount>=recipe.Amount);
};
foreach(配方中的配方)
{
if(全部配方(项目价格更正))
添加(配方);
}

将所有内容捆绑到
foreach
中有点。。。令人困惑

对于维护,我会使用
Func
使其更易于维护

Func<Recipe, bool> pricesAreCorrectForItem = (recipe) =>
     {
          var validInventoryItems = inventory.Inventory.Where(x=>x!=null);
          var item = validInventoryItems.FirstOrDefault(x=>x.ID == recipe.ID);
          return item == null ? false : (item.Amount >= recipe.Amount);
     };

foreach(Recipe recipe in recipes)
{
    if(recipe.All(pricesAreCorrectForItem))
        ableToCraft.Add(recipe);
}
Func pricesarecocorrectforitem=(配方)=>
{
var validInventoryItems=inventory.inventory.Where(x=>x!=null);
var item=validInventoryItems.FirstOrDefault(x=>x.ID==recipe.ID);
返回项==null?false:(item.Amount>=recipe.Amount);
};
foreach(配方中的配方)
{
if(全部配方(项目价格更正))
添加(配方);
}

x!=null&&x.ID==i.ID
?谢谢,我在第一个默认值之前添加了Where函数,它也可以工作,但我宁愿使用您的答案。
x!=null&&x.ID==i.ID
?谢谢,我在第一个默认值之前添加了Where函数,它也可以工作,但我宁愿使用您的答案。