Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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# LINQ OrderBy-自定义_C#_Linq - Fatal编程技术网

C# LINQ OrderBy-自定义

C# LINQ OrderBy-自定义,c#,linq,C#,Linq,我有一些数据,比如 ID Sequence customIndex 1 1 0 2 2 0 3 3 2 4 4 1 5 5 0 当customIndex为零时,我需要按顺序使用sequence,否则请使用customIndex 所以结果应该是ID,顺序为1,2,4,3,5 我需要使用Lambda实现LINQ。我尝试了一些解决方案,但无法实施 张贴重复和删除前一个,因为

我有一些数据,比如

ID    Sequence  customIndex
1     1         0
2     2         0
3     3         2
4     4         1
5     5         0
当customIndex为零时,我需要按顺序使用sequence,否则请使用customIndex

所以结果应该是ID,顺序为1,2,4,3,5

我需要使用Lambda实现LINQ。我尝试了一些解决方案,但无法实施

张贴重复和删除前一个,因为错误的格式问题的意义得到了改变,我收到了一堆反对票

在dotnet fiddle上添加了代码:


这适用于提供的测试数据:

    l.OrderBy(a => a.customIndex != 0 ? 
        list.Where(b => b.Sequence < a.Sequence && b.customIndex == 0)
            .OrderByDescending(c => c.Sequence)
            .FirstOrDefault()
            .Sequence : a.Sequence)
     .ThenBy(c=>c.customIndex )
     .ToList();
l.OrderBy(a=>a.customIndex!=0?
其中(b=>b.Sequencec.Sequence)
.FirstOrDefault()
.顺序:a.顺序)
.ThenBy(c=>c.customIndex)
.ToList();

其思想是先按前面的零值行,然后按非零值本身对非零值进行排序。

您要进行的排序(部分按CustomIndex排序,部分按顺序排序)不是这样的。但这应该接近你想要的。先按CustomIndex排序,然后按顺序排序

var result = data.OrderBy(x => x.CustomIndex).ThenBy(x => x.Sequence);

这是我想要的:

    public static void Main()
{
    List<Data> data = new List<Data>();
    data.Add(new Data{ Id=1, Sequence=1, CustomIndex=0});
    data.Add(new Data{ Id=5, Sequence=5, CustomIndex=0});
    data.Add(new Data{ Id=6, Sequence=6, CustomIndex=2});
    data.Add(new Data{ Id=2, Sequence=2, CustomIndex=0});
    data.Add(new Data{ Id=3, Sequence=3, CustomIndex=2});
    data.Add(new Data{ Id=4, Sequence=4, CustomIndex=1});

    data.Add(new Data{ Id=7, Sequence=7, CustomIndex=1});

    int o = 0;
    var result = data
        .OrderBy(x=>x.Sequence).ToList()
        .OrderBy((x)=> myCustomSort(x, ref o) )
            ;       
    result.ToList().ForEach(x=> Console.WriteLine(x.Id));
}

public static float myCustomSort(Data x, ref int o){

    if(x.CustomIndex==0){
        o = x.Sequence;
        return x.Sequence ;
    }
    else 
        return float.Parse(o + "."+ x.CustomIndex);
}
publicstaticvoidmain()
{
列表数据=新列表();
Add(新数据{Id=1,Sequence=1,CustomIndex=0});
Add(新数据{Id=5,Sequence=5,CustomIndex=0});
Add(新数据{Id=6,Sequence=6,CustomIndex=2});
Add(新数据{Id=2,Sequence=2,CustomIndex=0});
Add(新数据{Id=3,Sequence=3,CustomIndex=2});
Add(新数据{Id=4,Sequence=4,CustomIndex=1});
Add(新数据{Id=7,Sequence=7,CustomIndex=1});
INTO=0;
var结果=数据
.OrderBy(x=>x.Sequence).ToList()
.OrderBy((x)=>myCustomSort(x,ref o))
;       
result.ToList().ForEach(x=>Console.WriteLine(x.Id));
}
公共静态浮点myCustomSort(数据x,参考整数o){
如果(x.CustomIndex==0){
o=x.序列;
返回x.序列;
}
其他的
返回float.Parse(o+“+x.CustomIndex);
}
示例代码:
我将进一步完善它,它并不漂亮,但它体现了您的要求,我认为:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<Data> data = new List<Data>();
        data.Add(new Data { Id = 1, Sequence = 1, CustomIndex = 0 });
        data.Add(new Data { Id = 2, Sequence = 2, CustomIndex = 0 });
        data.Add(new Data { Id = 3, Sequence = 3, CustomIndex = 2 });
        data.Add(new Data { Id = 4, Sequence = 4, CustomIndex = 1 });
        data.Add(new Data { Id = 5, Sequence = 5, CustomIndex = 0 });

        //List of items where the sequence is what counts
        var itemsToPlaceBySequence = data.Where(x => x.CustomIndex == 0).OrderBy(x => x.Sequence).ToList();
        //List of items where the custom index counts
        var itemsToPlaceByCustomIndex = data.Where(x => x.CustomIndex != 0).OrderBy(x => x.CustomIndex).ToList();

        //Array to hold items
        var dataSlots = new Data[data.Count];

        //Place items by sequence
        foreach(var dataBySequence in itemsToPlaceBySequence) {
            dataSlots[dataBySequence.Sequence - 1] = dataBySequence ;
        }

        //Find empty data slots and place remaining items in CustomIndex order 
        foreach (var dataByCustom in itemsToPlaceByCustomIndex) {
            var index = dataSlots.ToList().IndexOf(null);
            dataSlots[index] = dataByCustom ;
        }

        var result = dataSlots.ToList();

        result.ForEach(x => Console.WriteLine(x.Id));
        var discard = Console.ReadKey();
    }

    public class Data
    {
        public int Id { get; set; }
        public int Sequence { get; set; }
        public int CustomIndex { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共课程
{
公共静态void Main()
{
列表数据=新列表();
Add(新数据{Id=1,Sequence=1,CustomIndex=0});
Add(新数据{Id=2,Sequence=2,CustomIndex=0});
Add(新数据{Id=3,Sequence=3,CustomIndex=2});
Add(新数据{Id=4,Sequence=4,CustomIndex=1});
Add(新数据{Id=5,Sequence=5,CustomIndex=0});
//顺序重要的项目列表
var itemsToPlaceBySequence=data.Where(x=>x.CustomIndex==0).OrderBy(x=>x.Sequence.ToList();
//自定义索引计数的项目列表
var itemsToPlaceByCustomIndex=data.Where(x=>x.CustomIndex!=0).OrderBy(x=>x.CustomIndex).ToList();
//用于保存项目的数组
var dataSlots=新数据[Data.Count];
//按顺序放置项目
foreach(ItemStopPlaceBySequence中的var数据序列){
dataSlots[DataySequence.Sequence-1]=DataySequence;
}
//找到空的数据槽,并按CustomIndex顺序放置剩余的项目
foreach(ItemStopPlaceByCustomIndex中的var dataByCustom){
var index=dataSlots.ToList().IndexOf(null);
dataslot[index]=dataByCustom;
}
var result=dataSlots.ToList();
result.ForEach(x=>Console.WriteLine(x.Id));
var discard=Console.ReadKey();
}
公共类数据
{
公共int Id{get;set;}
公共整数序列{get;set;}
public int CustomIndex{get;set;}
}
}

答案基于假设,
CustomIndex
大于或等于零:

var result = 
    data.OrderBy(x => x.CustomIndex==0 ? x.Sequence :
            data.Where(y => y.CustomIndex==0 && y.Sequence < x.Sequence)
                .Max(y => (int?)y.Sequence))
        .ThenBy(x => x.CustomIndex);
var结果=
data.OrderBy(x=>x.CustomIndex==0?x.序列:
其中(y=>y.CustomIndex==0&&y.Sequence(int?)y.Sequence)
.ThenBy(x=>x.CustomIndex);

根据你的问题和对我的评论的回答,我知道你需要对项目集合进行聚类,然后在每个集群的所有项目上考虑序列和自定义索引。 一旦聚集(根据特定的标准划分为块),您可以将它们合并回一个唯一的集合,但在这样做的同时,您可以按照需要的方式独立地操作每个集群

public static class extCluster
{
    public static IEnumerable<KeyValuePair<bool, T[]>> Clusterize<T>(this IEnumerable<T> self, Func<T, bool> clusterizer)
    {
        // Prepare temporary data
        var bLastCluster = false;
        var cluster = new List<T>();

        // loop all items
        foreach (var item in self)
        {
            // Compute cluster kind
            var bItemCluster = clusterizer(item);
            // If last cluster kind is different from current
            if (bItemCluster != bLastCluster)
            {
                // If previous cluster was not empty, return its items
                if (cluster.Count > 0)
                    yield return new KeyValuePair<bool, T[]>(bLastCluster, cluster.ToArray());

                // Store new cluster kind and reset items
                bLastCluster = bItemCluster;
                cluster.Clear();
            }

            // Add current item to cluster
            cluster.Add(item);
        }

        // If previous cluster was not empty, return its items
        if (cluster.Count > 0)
            yield return new KeyValuePair<bool, T[]>(bLastCluster, cluster.ToArray());
    }
}
// sample
static class Program
{
    public class Item
    {
        public Item(int id, int sequence, int _customIndex)
        {
            ID = id; Sequence = sequence; customIndex = _customIndex;
        }
        public int ID, Sequence, customIndex;
    }
    [STAThread]
    static void Main(string[] args)
    {
        var aItems = new[]
        {
            new Item(1, 1, 0),
            new Item(2, 2, 0),
            new Item(3, 3, 2),
            new Item(4, 4, 1),
            new Item(5, 5, 0)
        };

        // Split items into clusters
        var aClusters = aItems.Clusterize(item => item.customIndex != 0);
        // Explode clusters and sort their items
        var result = aClusters
            .SelectMany(cluster => cluster.Key
            ? cluster.Value.OrderBy(item => item.customIndex)
            : cluster.Value.OrderBy(item => item.Sequence));
    }
}
公共静态类extCluster
{
公共静态IEnumerable Clusterize(此IEnumerable self,Func clusterizer)
{
//准备临时数据
var-bLastCluster=false;
var cluster=新列表();
//循环所有项目
foreach(自身中的var项目)
{
//计算集群类型
var bItemCluster=聚类器(项目);
//如果最后一个群集类型不同于当前群集类型
if(bItemCluster!=bLastCluster)
{
//如果上一个集群不是空的,则返回其项目
如果(cluster.Count>0)
返回新的KeyValuePair(bLastCluster,cluster.ToArray());
//存储新的群集种类和重置项
bLastCluster=bItemCluster;
cluster.Clear();
}
//将当前项添加到集群
增加(项目);
}
//如果上一个集群不是空的,则返回其项目
如果(cluster.Count>0)
返回新的KeyValuePair(bLastCluster,cluster.ToArray());
}
}
//样品
静态类程序
{
公共类项目
{
公共项(int-id、int-sequence、int\u-customIndex)
{
ID=ID;Sequence=Sequence;customIndex=\u customIndex;
}
公共int-ID、序列、customIndex;
}
[状态线程]
静止的