排序在C# [英] sort a queue in C#

查看:266
本文介绍了排序在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下课程

  public enum ChangeType 
{
Add,
Modify,
Delete
}



public enum ChangedObjectType
{
项目,
客户,
边框,
照片
}

public struct ChangeInfo
{
public ChangeType typeofChange {get;私人集}
public ChangedObjectType objectType {get;私人集}

public string objectID {get;私人集}

public ChangeInfo(ChangeType changeType,ChangedObjectType changeObj,string objectId):this()
{
typeofChange = changeType;
objectType = changeObj;
objectID = objectId;
}

}

线程:

  public class ChangeInfoUploader 
{
static Queue< ChangeInfo> changeInfoQueue = new Queue< ChangeInfo>();
static Thread changeInfoUploaderThread = new Thread(new ThreadStart(ChangeInfoUploaderProc));
static bool isStarted = false;
static项目currentProject;

public static void初始化(项目curproject)
{
currentProject = curproject;
isStarted = true;
changeInfoUploaderThread.Start();
ResumeData();
}

static void ChangeInfoUploaderProc()
{
while(isStarted)
{
if(currentProject!= null)
{
ChangeInfo? addToDb = null;

//在dequeue之前需要对changeInfoQueue进行排序
lock(changeInfoQueue)
{
if(changeInfoQueue.Count!= 0)
addToDb = changeInfoQueue .Dequeue();
}
}
}
Logdata();
changeInfoUploaderThread.Abort();
}
}

这里是changeInfoQueue队列的示例数据。 p>

 < Info TypeofChange =AddObjectType =CustomerObjectId =0005/> 
< Info TypeofChange =AddObjectType =CustomerObjectId =0006/>
< Info TypeofChange =AddObjectType =CustomerObjectId =0007/>
< Info TypeofChange =AddObjectType =PhotoObjectId =01a243f5-4894-4d99-8238-9c4cd3/>

我的问题:




  • 我需要根据ObjectType来整理changeInfoQueue。



我的发现:




  • 我找到 OrderBy 。是可以使用吗?如果是,如何?



除此之外,我找到了priorityQueue。对我来说最好的解决方案是什么?



编辑



在创建相关对象时添加此队列。 (项目,边框等),并将其保存在本地XML文件中。之后,它需要写入数据库。这是通过使用线程来实现的,当我们保存这些数据时,必须以特定的顺序保存,以避免外键违规。所以这个线程用于调用这些相关的方法。



我使用orderby如下:

 队列< ChangeInfo> changeInfoQueue2 = changeInfoQueue.OrderBy(ChangeInfo => ChangeInfo.ObjectType); 

那么它会引发以下异常:



无法将类型System.Linq.IOrderedEnumerable隐式转换为System.Collections.Generic.Queue。显示转换是否存在(是否缺少转换?)

解决方案

为什么要按对象类型排序在队列中?一个队列,根据它的定义,它不意味着以这种方式排序,但打算作为一个先进先出类型的元素。



使用List你只需要一个能够被排序的集合,并且为你拥有的不同类型的对象创建多个队列。



例如,如果你去超级市场,你有几个队列,每个不同的部分...一个人没有任何意义,把所有的人在同一个队列,然后订单他们,是否是为了屠夫或面包店。 / p>

你需要一个队列,当你需要队列的东西...如果你不使用适当的结构,不要尝试强制它进入队列。 (如果你有一把锤子一切看起来像一个钉...但它不应该)


Even though this question sounds as a duplicate, I searched a lot but couldn't find a proper solution.

I have following classes

public enum ChangeType
{ 
    Add,
    Modify,
    Delete
}



public enum ChangedObjectType
{ 
    Project,
    Customer,
    Border,
    Photo
}

public struct ChangeInfo
{
    public ChangeType typeofChange { get; private set; }
    public ChangedObjectType objectType { get; private set; }

    public string objectID { get; private set; }

    public ChangeInfo(ChangeType changeType, ChangedObjectType changeObj, string objectId):this()
    {
        typeofChange = changeType;
        objectType = changeObj;
        objectID = objectId;
    }

}

thread :

public class ChangeInfoUploader
{ 
    static Queue<ChangeInfo> changeInfoQueue = new Queue<ChangeInfo>();
    static Thread changeInfoUploaderThread = new Thread(new ThreadStart(ChangeInfoUploaderProc));
    static bool isStarted = false;
    static Project currentProject;

    public static void Initialize(Project curproject)
    {
        currentProject = curproject;
        isStarted = true;
        changeInfoUploaderThread.Start();
        ResumeData();
    }

    static void ChangeInfoUploaderProc()
    {
        while (isStarted)
        {
            if (currentProject != null)
            {
                ChangeInfo? addToDb = null;

             // I need to sort changeInfoQueue before dequeue
                lock (changeInfoQueue)
                {
                    if (changeInfoQueue.Count != 0)
                        addToDb = changeInfoQueue.Dequeue();
                }
            }
        }
        Logdata();
        changeInfoUploaderThread.Abort();
    }
}

here is the sample data of changeInfoQueue queue.

<Info TypeofChange="Add" ObjectType="Customer" ObjectId="0005" />
<Info TypeofChange="Add" ObjectType="Customer" ObjectId="0006" />
<Info TypeofChange="Add" ObjectType="Customer" ObjectId="0007" />
<Info TypeofChange="Add" ObjectType="Photo" ObjectId="01a243f5-4894-4d99-8238-9c4cd3" />

My Question :

  • I need to sort out changeInfoQueue based on ObjectType. How can i do that?

My findings:

  • I found OrderBy . Is it possible to use it? If so, how?

In addition to that I found priorityQueue. What is the best solution for me?

EDIT:

The values of this queue are added when relevant objects are created. (projects, borders etc.) and saves it in a local XML file. After that it needs to write to a database. This is accomplished by using a thread and when we save this data it must be saved in particular order to avoid foreign key violations. So this thread is used to call those relevant methods.

I used orderby as follows:

Queue<ChangeInfo> changeInfoQueue2 = changeInfoQueue.OrderBy(ChangeInfo => ChangeInfo.ObjectType);

then it throws following exception:

Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Collections.Generic.Queue'. An explicit conversion exists (are you missing a cast?)

解决方案

Why would you want to order by the type of object in a queue? A queue, by its definition, it's not meant to be ordered in that way but intended to work as a first in first out kind of element.

Either use a List if you just want a collection capable of being ordered, and ordered list or create several queues for the different kind of objects that you have.

For example, if you go to the super market you have several queues, one for each different section... it wouldn't make any sense to put all people in the same queue and then "order" them based on whether they are in for the butcher or the bakery.

You have a queue when you need to "queue" things... if you don't use the appropriate construct, don't try to force it into a queue. ("if you have a hammer everything looks like a nail"... but it should not)

这篇关于排序在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆