Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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# 更新列表列表中的属性_C#_List - Fatal编程技术网

C# 更新列表列表中的属性

C# 更新列表列表中的属性,c#,list,C#,List,问题是:如何在没有循环的情况下(例如使用Linq)更新allItems中的字段ItemCount?例如,如果Index=0,我想将ItemCount设置为ItemCount-5 您可以看到下面的代码 我有以下课程: public class Item { private int index; private string item_name; private double item_count; private string type_name; pub

问题是:如何在没有循环的情况下(例如使用Linq)更新allItems中的字段ItemCount?例如,如果Index=0,我想将ItemCount设置为ItemCount-5

您可以看到下面的代码

我有以下课程:

public class Item
{
    private int index;
    private string item_name;
    private double item_count;
    private string type_name;

    public int Index
    {
        get { return index; }
        set { index = value; }
    }
    public string ItemName
    {
        get { return item_name; }
        set { item_name = value; }
    }
    public double ItemCount
    {
        get { return item_count; }
        set { item_count = value; }
    }
    public string TypeName
    {
        get { return type_name; }
        set { type_name = value; }
    }

}

public class CombMin
{
    private double min_count;
    private List<Item> combination;

    public double MinCount
    {
        get { return min_count; }
        set { min_count = value; }
    }
    public List<Item> Combination
    {
        get { return combination; }
        set { combination = value; }
    }
}
公共类项目
{
私有整数索引;
私有字符串项目名称;
私人双项目计数;
私有字符串类型\u名称;
公共整数索引
{
获取{返回索引;}
设置{index=value;}
}
公共字符串ItemName
{
获取{return item_name;}
设置{item_name=value;}
}
公共重复项目计数
{
获取{return item_count;}
设置{item_count=value;}
}
公共字符串类型名
{
获取{return type_name;}
设置{type_name=value;}
}
}
公共类CombMin
{
私人双重最小计数;
私有列表组合;
公共双重计数
{
获取{return minu count;}
设置{min_count=value;}
}
公共列表组合
{
获取{返回组合;}
设置{combination=value;}
}
}
然后我创建了列表所有项:

        var item1 = new Item { Index=0,ItemName="x1",ItemCount=104,TypeName="Type1" };
        var item2 = new Item { Index = 1, ItemName = "x2", ItemCount = 104, TypeName = "Type1" };
        var item3 = new Item { Index = 2, ItemName = "x3", ItemCount = 1495, TypeName = "Type1" };
        var item4 = new Item { Index = 0, ItemName = "x1", ItemCount = 104, TypeName = "Type1" };
        var item5 = new Item { Index = 1, ItemName = "x2", ItemCount = 104, TypeName = "Type1" };
        var item6 = new Item { Index = 2, ItemName = "x3", ItemCount = 1495, TypeName = "Type1" };

        var items1 = new List<Item>();
        var items2 = new List<Item>();
        items1.Add(item1);
        items1.Add(item2);
        items1.Add(item3);

        items2.Add(item4);
        items2.Add(item5);
        items2.Add(item6);

        var allItems = new List<CombMin>();
        allItems.Add(new CombMin { Combination = items1, MinCount = 104 });
        allItems.Add(new CombMin { Combination = items2, MinCount = 104 });
var item1=newitem{Index=0,ItemName=“x1”,ItemCount=104,TypeName=“Type1”};
var item2=新项目{Index=1,ItemName=“x2”,ItemCount=104,TypeName=“Type1”};
var item3=新项目{Index=2,ItemName=“x3”,ItemCount=1495,TypeName=“Type1”};
var item4=新项{Index=0,ItemName=“x1”,ItemCount=104,TypeName=“Type1”};
var item5=新项目{Index=1,ItemName=“x2”,ItemCount=104,TypeName=“Type1”};
var item6=新项目{Index=2,ItemName=“x3”,ItemCount=1495,TypeName=“Type1”};
var items1=新列表();
var items2=新列表();
第1项。增加(第1项);
项目1.增加(项目2);
项目1.增加(项目3);
项目2.增加(项目4);
项目2.增加(项目5);
第2项增加(第6项);
var allItems=新列表();
Add(new-CombMin{Combination=items1,MinCount=104});
Add(new-CombMin{Combination=items2,MinCount=104});
问题是:如何在没有循环的情况下(使用Linq)更新allItems中的字段ItemCount?例如,如果Index=0,我想将ItemCount设置为ItemCount-5。

基本解决方案:

allItems.ForEach(x => x.Combination.FirstOrDefault(ele => ele.Index == 0).ItemCount -= 5);
如注释中所述,如果只有1个元素的索引为1,则可以执行以下操作:

如果不确定集合中是否存在给定索引,则可以执行以下操作:


Linq用于查询而不是更新记录。因此,首先使用linq获取记录,然后迭代收集和更新

在您的情况下,它应该是:

foreach (var x in allItems)
{
    foreach (var y in x.Combination)
    {
        if (y.Index == 0)
        {
            y.ItemCount = 105;
        }
    }
}
是的,应该使用LINQ查询某些内容,而不是修改它。但他可以使用LINQ来确定需要更新的内容。这是我最喜欢的:

IEnumerable<Item> itemsToUpdateWithIndex0 = allItems
    .SelectMany(c => c.Combination)
    .Where(item => item.Index == 0);

foreach (Item item in itemsToUpdateWithIndex0)
    item.ItemCount -= 5;
IEnumerable itemsToUpdateWithIndex0=allItems
.SelectMany(c=>c.composition)
其中(项=>项索引==0);
foreach(itemsToUpdateWithIndex0中的项目)
item.ItemCount-=5;

如果没有
索引==0的项,这将引发异常。处理case@TimSchmelter这是对的。如果没有任何项具有
索引==0
FirstOrDefault
将重新运行
null
,这将在尝试设置
ItemCount
ForEach is a Loop时为您提供
NullReferenceException
。。哈哈。但是我相信作者只是想避免自己做循环。顺便说一句,它的ItemCount=ItemCount-5;如果总是有一个项目的
Index==0
,则可以使用
First
而不是
FirstOrDefault
。如果没有,将抛出(如前所述),如果有多个,则只更新第一个。@Lucifer:当然,但此类中的任何内容都不会避免重复索引或确保索引为0的索引。您可以使用
Any
来检查是否有循环,但是您会执行两次查询,这将是低效的。“没有循环(使用Linq)”所以您认为Linq不使用循环?如果您没有在属性上实现自定义get和setter,为什么不在属性之后使用{get;set;}?这将使你的代码变得更加干净。
foreach (var x in allItems)
{
    foreach (var y in x.Combination)
    {
        if (y.Index == 0)
        {
            y.ItemCount = 105;
        }
    }
}
IEnumerable<Item> itemsToUpdateWithIndex0 = allItems
    .SelectMany(c => c.Combination)
    .Where(item => item.Index == 0);

foreach (Item item in itemsToUpdateWithIndex0)
    item.ItemCount -= 5;