English 中文(简体)
排序 C 的 Linq #
原标题:Linq for sorting C#
  • 时间:2012-05-24 18:36:19
  •  标签:
  • c#
  • linq

天气非常炎热,所以我的大脑没有工作那么好。 与 LINQ 相比, 什么是最好的排序方法? 注意根据“ C” 进行排序, 但应用到“ M ” 。

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

namespace Compare
{
    public class M
    {
        public IList<C> Columns = new List<C>();
    }

    public class C
    {
        public bool SortByMe { get; set; }
        public string Guid { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IList<M> list = new List<M>();

            M one = new M();
            one.Columns.Add(new C() 
            {
                SortByMe = true,
                Guid = "5"
            });
            one.Columns.Add(new C()
            {                
            });
            one.Columns.Add(new C()
            {             
            });

            M two = new M();
            two.Columns.Add(new C()
            {             
            });
            two.Columns.Add(new C()
            {
                SortByMe = true,
                Guid = "2"
            });
            two.Columns.Add(new C()
            {             
            });

            M three = new M();
            three.Columns.Add(new C()
            {

            });
            three.Columns.Add(new C()
            {
                SortByMe = true,
                Guid = "100"
            });
            three.Columns.Add(new C()
            {

            });

            list.Add(one);
            list.Add(two);
            list.Add(three);

            //Then sort the M by the occurrence of a C with SortByMe true.

        }
    }
}
最佳回答

你是说每个 M 实例都将有一个 single C 实例,包括 Columns 收藏,其 SortByMe 属性是 > true ;你要通过这些 M 元素的 GUID 值排序你收藏的 M 实例?

list = list.OrderBy(m => m.Columns.Single(c => c.SortByMe).Guid).ToList();

请注意,由于 GUID string , 您的排序将按字母顺序( "100" , 2" , < , 5" ), 而非数字。 如果您想要数字, 您需要投进一个 int. arse

< 加固 > 编辑 : 版本进行数字排序 :

list = list.OrderBy(m => int.Parse(m.Columns.Single(c => c.SortByMe).Guid)).ToList();

逻辑等效查询语法 :

list =
(
    from m in list
    orderby
    (
        from c in m.Columns
        where c.SortByMe
        select int.Parse(c.Guid)
    ).Single()
    select m
).ToList();

您需要考虑到任何原始假设的失败 — — 例如,在给定的 Columns 收藏中有多个 Columns 元素且其 SortByMe 设置为 ret ,或者一个不有效的整数的 GUID 值,都会导致整个表达式失效。

问题回答
        var query = from m in list
                    let c = m.First(x => x.SortByMe)
                    orderby c.Guid
                    select m;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...