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
C# 使用Linq将包含基本体数据的对象列表分组到新的分组对象列表中_C#_List_Linq - Fatal编程技术网

C# 使用Linq将包含基本体数据的对象列表分组到新的分组对象列表中

C# 使用Linq将包含基本体数据的对象列表分组到新的分组对象列表中,c#,list,linq,C#,List,Linq,我有一个包含不同基本数据类型的对象列表: List<object> list = new List<object>() { 3, "cat", 4.1, 'a', 5, "dog", "horse", 9.1, 'd', 1 }; 我试过了 代码 工作,但不提供所需的输出 输出 { 3, 5, 1, "cat","dog", "horse",4.1, 9.1, 'a', 'd' } 如果问题是输出中类型的顺序,则可以使用自定义IComparer类对列表进行排

我有一个包含不同基本数据类型的对象列表:

List<object> list = new List<object>() { 
  3, "cat", 4.1, 'a', 5, "dog", "horse", 9.1, 'd', 1 
};
我试过了

代码

工作,但不提供所需的输出

输出

 { 3, 5, 1, "cat","dog", "horse",4.1, 9.1, 'a', 'd' }

如果问题是输出中类型的顺序,则可以使用自定义IComparer类对列表进行排序

基本原则是可以将类型映射到整数分数,然后返回分数之间的比较

像这样使用它:

        List<object> list = new List<object>() { 3, "cat", 4.1, 'a', 5, "dog", "horse", 9.1, 'd', 1 };

        list.Sort(new StringThenIntThenDoubleThenChar());
        list.ForEach(x => Console.WriteLine(x));
与已经给出的按类型排序名称的更简单解决方案相比,它的优势在于您可以根据需要对其进行自定义


您还可以优化比较器,例如,如果分数相等,则可以使用默认的比较顺序对其进行比较,以便分别对字符串、int等进行排序。

您可以在选择后通过降序对类型名称进行排序,它将为您提供所需的输出-字符串、整数、双精度、字符项

list.GroupByx=>x.GetType .OrderByDownering=>g.Key.Name .Selectgrp=>grp.ToList 托利斯先生 .ForEachgroup=>group.ForEachConsole.WriteLine; 另一个选项是order by property OrderByg=>g.Key.IsPrimitive,对于您的特定数据,它还提供了所需的输出

类似的回答方法,您可以创建一个方法来决定类型的排序顺序:

private static int OrderByOnType(object o)
{
    var type = o.GetType();

    if (type == typeof(string))
        return 0;

    else if (type == typeof(int)) 
        return 1;

    else if (type == typeof(double)) 
        return 2;

    else if (type == typeof(char)) 
        return 3;

    return 4;
}
然后将此方法传递给from LINQ以创建新的排序列表:

var result = list.OrderBy(OrderByOnType);

Console.WriteLine("{ " + string.Join(", ", result) + " }");
或通过传递您自己的,将原始列表排序到位:

也可以将其包装到单独的方法中:

private static int CompareTwoTypes(object x, object y)
{
    return OrderByOnType(x).CompareTo(OrderByOnType(y));
}

list.Sort(CompareTwoTypes);

Console.WriteLine("{ " + string.Join(", ", list) + " }");
所有这些都提供了:

{ cat, dog, horse, 3, 5, 1, 4.1, 9.1, a, d }

我认为and的较短版本是在Linq查询中使用switch表达式

        list.OrderBy(x => x  switch
        {
             string xString=> 0,
             int xInt=> 1,
             double xDouble=> 2,
             char xChar=> 3,
            _ => -1,
        }).ToList().ForEach(item => Console.WriteLine(item));

您的表达式通常可以工作,您想要哪种输出?也许只是按类型排序?基本上你想把你的列表聚合成一个列表。当然,ForEach向您展示了它的正确要点。@PavelAnikhouski我想将输出重新排序为string、integer、double、character而不是integer、string、double、character,因此您需要使用自定义比较器和SortIf,如果string、integer、double、character是按类型名称降序排列的,list.orderByDescendaningx=>x.GetType.Name应该这样做
list.Sort((x, y) => OrderByOnType(x).CompareTo(OrderByOnType(y)));

Console.WriteLine("{ " + string.Join(", ", list) + " }");
private static int CompareTwoTypes(object x, object y)
{
    return OrderByOnType(x).CompareTo(OrderByOnType(y));
}

list.Sort(CompareTwoTypes);

Console.WriteLine("{ " + string.Join(", ", list) + " }");
{ cat, dog, horse, 3, 5, 1, 4.1, 9.1, a, d }
        list.OrderBy(x => x  switch
        {
             string xString=> 0,
             int xInt=> 1,
             double xDouble=> 2,
             char xChar=> 3,
            _ => -1,
        }).ToList().ForEach(item => Console.WriteLine(item));