Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/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# - Fatal编程技术网

C# 合并长列表

C# 合并长列表,c#,C#,假设我有一个类似这样的对象模型: public class MyModel { public List<long> TotalItems { get; set; } public List<long> ItemsApples { get; set; } public List<long> ItemsOranges { get; set; } public List<long> ItemsPeaches { get;

假设我有一个类似这样的对象模型:

public class MyModel
{
    public List<long> TotalItems { get; set; }
    public List<long> ItemsApples { get; set; }
    public List<long> ItemsOranges { get; set; }
    public List<long> ItemsPeaches { get; set; } 

    public void CombineItems()
    {

    }
}
公共类MyModel
{
公共列表TotalItems{get;set;}
公共列表项共享{get;set;}
公共列表项范围{get;set;}
公共列表项缓存{get;set;}
公共无效组合项()
{
}
}
实际上,模型中大约有14个多头列表。组合这些列表的最佳方式是什么,以便TotalItems是所有其他列表的组合列表

感谢您的建议。

创建一个新的
列表
,然后调用
AddRange()
将每个现有列表添加到其中。

创建一个新的
列表
,然后调用
AddRange()
将每个现有列表添加到其中。

使用System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;

public class MyModel
{
    public List<long> TotalItems
    {
        get
        {
            return ItemsApples.Concat(ItemsOranges).Concat(ItemsPeaches).ToList(); // all lists conbined, including duplicates
            //return ItemsApples.Union(ItemsOranges).Union(ItemsPeaches).ToList(); // set of all items
        }
    }

    public List<long> ItemsApples { get; set; }

    public List<long> ItemsOranges { get; set; }

    public List<long> ItemsPeaches { get; set; }

    public void CombineItems()
    {

    }
}
使用System.Linq; 公共类MyModel { 公共物品清单 { 得到 { 返回ItemsApples.Concat(ItemsOranges).Concat(ItemsPeaches.ToList();//合并的所有列表,包括重复列表 //return ItemsApples.Union(ItemsOranges.Union(ItemsPeaches.ToList();//所有项的集合 } } 公共列表项共享{get;set;} 公共列表项范围{get;set;} 公共列表项缓存{get;set;} 公共无效组合项() { } }
使用System.Collections.Generic;
使用System.Linq;
公共类MyModel
{
公共物品清单
{
得到
{
返回ItemsApples.Concat(ItemsOranges).Concat(ItemsPeaches.ToList();//合并的所有列表,包括重复列表
//return ItemsApples.Union(ItemsOranges.Union(ItemsPeaches.ToList();//所有项的集合
}
}
公共列表项共享{get;set;}
公共列表项范围{get;set;}
公共列表项缓存{get;set;}
公共无效组合项()
{
}
}

除非您同时需要所有项目(而不是枚举),否则我会这样做:

public IEnumerable<long> TotalItems 
{
    get 
    {
        foreach(var i in ItemsApples) 
            yield return i;
        foreach(var i in ItemsOranges)
            yield return i;
        foreach(var i in ItemsPeaches)
            yield return i;
    }
}
public IEnumerable TotalItems
{
得到
{
foreach(ItemsApples中的变量i)
收益率i;
foreach(ItemsOranges中的变量i)
收益率i;
foreach(ItemsPeaches中的变量i)
收益率i;
}
}

从这里开始,如果除了添加或删除long列表之外,您再也不想维护类,那么您可以从反射中获得一些乐趣:

public IEnumerable<long> TotalItems
{
    get
    {
        // this automatically discovers properties of type List<long>
        // and grabs their values
        var properties = from property in GetType().GetProperties()
                    where typeof(List<long>).IsAssignableFrom(property.PropertyType)
                    select (IEnumerable<long>)property.GetValue(this, null);

        foreach (var property in properties)
        {
            foreach (var value in property)
                yield return value;
        }
    }
}
public IEnumerable TotalItems
{
得到
{
//这将自动发现列表类型的属性
//抓住他们的价值观
var properties=来自GetType()中的属性。GetProperties()
其中typeof(List).IsAssignableFrom(property.PropertyType)
select(IEnumerable)property.GetValue(this,null);
foreach(属性中的var属性)
{
foreach(属性中的var值)
收益回报值;
}
}
}

除非您同时需要所有项目(而不是枚举),否则我会这样做:

public IEnumerable<long> TotalItems 
{
    get 
    {
        foreach(var i in ItemsApples) 
            yield return i;
        foreach(var i in ItemsOranges)
            yield return i;
        foreach(var i in ItemsPeaches)
            yield return i;
    }
}
public IEnumerable TotalItems
{
得到
{
foreach(ItemsApples中的变量i)
收益率i;
foreach(ItemsOranges中的变量i)
收益率i;
foreach(ItemsPeaches中的变量i)
收益率i;
}
}

从这里开始,如果除了添加或删除long列表之外,您再也不想维护类,那么您可以从反射中获得一些乐趣:

public IEnumerable<long> TotalItems
{
    get
    {
        // this automatically discovers properties of type List<long>
        // and grabs their values
        var properties = from property in GetType().GetProperties()
                    where typeof(List<long>).IsAssignableFrom(property.PropertyType)
                    select (IEnumerable<long>)property.GetValue(this, null);

        foreach (var property in properties)
        {
            foreach (var value in property)
                yield return value;
        }
    }
}
public IEnumerable TotalItems
{
得到
{
//这将自动发现列表类型的属性
//抓住他们的价值观
var properties=来自GetType()中的属性。GetProperties()
其中typeof(List).IsAssignableFrom(property.PropertyType)
select(IEnumerable)property.GetValue(this,null);
foreach(属性中的var属性)
{
foreach(属性中的var值)
收益回报值;
}
}
}

14个同一型号的多头列表?我想这可能是解决某些问题的理想方案,但我确实引起了我的注意。14一个模型上的长列表?我想这可能是一个理想的解决方案,但我确实引起了我的注意。AddRange是复制值还是仅仅创建引用?@frenchie:
long
是一种值类型;它将始终被复制。AddRange是要复制值还是只是创建引用?@frenchie:
long
是一种值类型;它总是会被复制的。像这样的实现应该作为一种方法,并且具有类型
IEnumerable
,因此调用方很清楚这是根据需要计算的。我们应该始终针对接口编码,并且应该使用名称空间,使用语句应该在名称空间块中…我无法理解您评论的意图。我认为省略号表示您对我的评论感到恼火?这样的实现应该作为一种方法,并且具有类型
IEnumerable
,因此调用者很清楚这是按需计算的。我们应该始终针对接口进行编码,并且应该使用名称空间,使用语句应该在名称空间块中…我无法理解您评论的意图。我想省略号表示你对我的评论感到恼火?