Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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/8/linq/3.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中创建合并项方法# //Items.cs 公共类项目 { 公共int ItemNumber{get;set;} 公共日期时间项日期{get;set;} 公共列表项行{get;set;}=new List(); ///MergeItem将sourceItem中的项追加到当前项 /// ///要从中合并的项目 公共无效合并项(项源项) { //IEnumerable items=新列表(); //IEnumerable items=ItemsLines.Union 抛出新的NotImplementedException(); } } //ItemLine.cs 公共类项目行 { public int ItemLineId{get;set;} 公共整数数量{get;set;} 公共双成本{get;set;} //公共双总计{get{返回this.Cost*this.Quantity;} 公共项目行(int itemLineId、int数量、双重成本) { this.ItemLineId=ItemLineId; 这个。数量=数量; 成本=成本; } public ItemsLine(){} } //Program.cs 私有静态无效合并项() { var item1=新项目(); item1.ItemsLine.Add(新的ItemsLine() { ItemLineId=1, 成本=10.33, 数量=4, }); var item2=新项目(); item2.ItemsLine.Add(新的ItemsLine() { ItemLineId=2, 成本=5.22, 数量=1, }); item2.ItemsLine.Add(新的ItemsLine() { ItemLineId=3, 成本=6.27, 数量=3, }); 项目1.合并项目(项目2); WriteLine(“合并后的总计={0}”,item1.ItemsLines.Sum(i=>i.Cost*i.Quantity)); }_C#_Linq_Console Application - Fatal编程技术网

C# 如何在c中创建合并项方法# //Items.cs 公共类项目 { 公共int ItemNumber{get;set;} 公共日期时间项日期{get;set;} 公共列表项行{get;set;}=new List(); ///MergeItem将sourceItem中的项追加到当前项 /// ///要从中合并的项目 公共无效合并项(项源项) { //IEnumerable items=新列表(); //IEnumerable items=ItemsLines.Union 抛出新的NotImplementedException(); } } //ItemLine.cs 公共类项目行 { public int ItemLineId{get;set;} 公共整数数量{get;set;} 公共双成本{get;set;} //公共双总计{get{返回this.Cost*this.Quantity;} 公共项目行(int itemLineId、int数量、双重成本) { this.ItemLineId=ItemLineId; 这个。数量=数量; 成本=成本; } public ItemsLine(){} } //Program.cs 私有静态无效合并项() { var item1=新项目(); item1.ItemsLine.Add(新的ItemsLine() { ItemLineId=1, 成本=10.33, 数量=4, }); var item2=新项目(); item2.ItemsLine.Add(新的ItemsLine() { ItemLineId=2, 成本=5.22, 数量=1, }); item2.ItemsLine.Add(新的ItemsLine() { ItemLineId=3, 成本=6.27, 数量=3, }); 项目1.合并项目(项目2); WriteLine(“合并后的总计={0}”,item1.ItemsLines.Sum(i=>i.Cost*i.Quantity)); }

C# 如何在c中创建合并项方法# //Items.cs 公共类项目 { 公共int ItemNumber{get;set;} 公共日期时间项日期{get;set;} 公共列表项行{get;set;}=new List(); ///MergeItem将sourceItem中的项追加到当前项 /// ///要从中合并的项目 公共无效合并项(项源项) { //IEnumerable items=新列表(); //IEnumerable items=ItemsLines.Union 抛出新的NotImplementedException(); } } //ItemLine.cs 公共类项目行 { public int ItemLineId{get;set;} 公共整数数量{get;set;} 公共双成本{get;set;} //公共双总计{get{返回this.Cost*this.Quantity;} 公共项目行(int itemLineId、int数量、双重成本) { this.ItemLineId=ItemLineId; 这个。数量=数量; 成本=成本; } public ItemsLine(){} } //Program.cs 私有静态无效合并项() { var item1=新项目(); item1.ItemsLine.Add(新的ItemsLine() { ItemLineId=1, 成本=10.33, 数量=4, }); var item2=新项目(); item2.ItemsLine.Add(新的ItemsLine() { ItemLineId=2, 成本=5.22, 数量=1, }); item2.ItemsLine.Add(新的ItemsLine() { ItemLineId=3, 成本=6.27, 数量=3, }); 项目1.合并项目(项目2); WriteLine(“合并后的总计={0}”,item1.ItemsLines.Sum(i=>i.Cost*i.Quantity)); },c#,linq,console-application,C#,Linq,Console Application,合并项目的规则是: 传递到Merge中的项目的ItemsLine中的ItemsLine实例添加到this.ItemsLine,或用于更新现有项目 ItemsLine具有列表中唯一的ItemLineID 如果列表中已存在incoming ItemsLine.ItmeLineID,则应将incoming item的数量添加到现有item中,并将现有成本调整为现有成本和传入成本的平均值 如果传入ItemsLine.ItemLineID在列表中不存在,则应添加ItemsLine 我试着使用下面的代码

合并项目的规则是:

  • 传递到
    Merge
    中的项目的
    ItemsLine
    中的
    ItemsLine
    实例添加到
    this.ItemsLine
    ,或用于更新现有项目
  • ItemsLine
    具有列表中唯一的ItemLineID
  • 如果列表中已存在incoming ItemsLine.ItmeLineID,则应将incoming item的数量添加到现有item中,并将现有成本调整为现有成本和传入成本的平均值
  • 如果传入ItemsLine.ItemLineID在列表中不存在,则应添加ItemsLine
我试着使用下面的代码,但没有成功

  • IEnumerable items=new List()

  • IEnumerable items=ItemsLines.Union()


要进行合并,我是否需要使用Concat?,因此您可以合并两个实现IEnumerable或Linq join查询的集合,以实现mergeritem?

您需要一个实现MergeItem的集合,该集合将本地项与传入项合并:

//Items.cs
public class Items
{
    public int ItemNumber { get; set; }
    public DateTime ItemDate { get; set; }
    public List<ItemsLine> ItemsLines { get; set; } = new List<ItemsLine>();

    /// MergeItem appends the items from the sourceItem to the current item
    /// </summary>
    /// <param name="sourceItem">Item to merge from</param>
    public void MergeItem( Items sourceItem)
    {
        //IEnumerable<ItemsLine> items = new List<ItemsLine>();
        //IEnumerable<Items> items = ItemsLines.Union<item>
        
        throw new NotImplementedException();
    }
}

//ItemLine.cs
public class ItemsLine
{
    public int ItemLineId { get; set; }
    public int Quantity { get; set; }
    public double Cost { get; set; }

    // public double Total { get { return this.Cost * this.Quantity; } }

    public ItemsLine(int itemLineId, int quantity, double cost)
    {
        this.ItemLineId = itemLineId;
        this.Quantity = quantity;
        this.Cost = cost;
    }

    public ItemsLine() { }
}

//Program.cs
private static void MergeItem()
{
    var item1 = new Items();

    item1.ItemsLines.Add(new ItemsLine()
    {
        ItemLineId = 1,
        Cost = 10.33,
        Quantity = 4,
    });

    var item2 = new Items();

    item2.ItemsLines.Add(new ItemsLine()
    {
        ItemLineId = 2,
        Cost = 5.22,
        Quantity = 1,
    });

    item2.ItemsLines.Add(new ItemsLine()
    {
        ItemLineId = 3,
        Cost = 6.27,
        Quantity = 3,
    });

    item1.MergeItem(item2);
    Console.WriteLine("Grand total after merge = {0}", item1.ItemsLines.Sum(i => i.Cost * i.Quantity));
}

请不要用复数形式命名你的类,这是一种不好的做法,因为如果变量是你的东西的集合,那么很难用复数形式命名变量。一个
列表
应该被称为
Itemss
(多个项目),这有点尴尬。如果您创建的类是某个东西的集合,而这些东西实际上没有一个人们理解为包含某个东西的倍数的公认名称(例如
列表书
订单
列表产品
等)然后用集合后缀或类似的名称来命名类,这意味着它是一组事物,但要小心,使类的目的实际上只不过是“成为事物的集合”,就像Microsoft对DataTable所做的那样(有一个名为Columns的DataColumnCollection和一个名为Rows的DataRowCollection——这些东西实际上就是这些东西的集合)

您询问如何使用“Linq连接查询”合并两个列表。从概念上讲,您提出的算法对应于两个
ItemsLine
列表上的a,在
ItemLineId
上匹配,按原样保留两个列表中的不匹配项,并使用问题中描述的逻辑合并匹配项:

(图由作者提供。)

不幸的是,目前LINQ中没有内置完整的外部联接。幸运的是,有多个正在工作的IME组件。我从中复制了
Ext.FullOuterJoin()
,并能够合并
列表,如下所示:

public void MergeItem(Item incoming)
{
    if(this.ItemNumber != incoming.ItemNumber)
      throw new BlahException($"Item with number {this.ItemNumber} cannot be merged with different item having number {incoming.ItemNumber}");

    this.ItemDate = DateTime.UtcNow;

    foreach(var il in incoming.ItemLines){

      var existing = this.ItemLines.SingleOrDefault(t => t.ItemLineId == il.ItemLineId);

      if(existing == null) //not known, add 
        this.ItemLines.Add(il);
      else{ //merge
        existing.Quantity += il.Quantity;
        existing.Cost = (existing.Cost + il.Cost) / 2.0;
      }
    }
}
公共类项目
{
公共int ItemNumber{get;set;}
公共日期时间项日期{get;set;}
公共列表项行{get;set;}=new List();
///MergeItem将sourceItem中的项追加到当前项
/// 
///要从中合并的项目
公共无效合并项(项源项)
{
var mergedItems=ItemsLines.FullOuterJoin(sourceItem.ItemsLines,i=>i.ItemLineId,i=>i.ItemLineId,(i1,i2)=>
{
如果(i1==默认值)
返回i2;
else if(i2==默认值)
返回i1;
//从你的问题来看,不清楚平均成本是否应该按数量加权。
//i1.成本=(i1.成本+i2.成本)/2.0,
i1.成本=(i1.数量*i1.成本+i2.数量*i2.成本)/(i1.数量+i2.数量);
i1.数量=i1.数量+i2.数量;
返回i1;
})
.ToList();
ItemsLines.Clear();
ItemsLines.AddRange(合并数据项);
}
}

正在使用演示小提琴。

您希望如何处理副本?如果此项目行的项目ID为1,数量为10,而您要合并的其他项目行的项目ID为1,数量为20,是否希望:一个ID1数量为30(合并添加),打开
public class Items
{
    public int ItemNumber { get; set; }
    public DateTime ItemDate { get; set; }
    public List<ItemsLine> ItemsLines { get; set; } = new List<ItemsLine>();

    /// MergeItem appends the items from the sourceItem to the current item
    /// </summary>
    /// <param name="sourceItem">Item to merge from</param>
    public void MergeItem( Items sourceItem)
    {
        var mergedItems = ItemsLines.FullOuterJoin(sourceItem.ItemsLines, i => i.ItemLineId, i => i.ItemLineId, (i1, i2) =>
                                                   {
                                                       if (i1 == default)
                                                           return i2;
                                                       else if (i2 == default)
                                                           return i1;
                                                       //It's not clear from your question whether the average cost should be weighted by the quantities.
                                                       //i1.Cost = (i1.Cost + i2.Cost) / 2.0,
                                                       i1.Cost = (i1.Quantity * i1.Cost + i2.Quantity * i2.Cost) / (i1.Quantity + i2.Quantity);
                                                       i1.Quantity = i1.Quantity + i2.Quantity;
                                                       return i1;
                                                   })
            .ToList();
        ItemsLines.Clear();
        ItemsLines.AddRange(mergedItems);
    }
}