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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/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# 基于外部属性的排序_C#_Linq - Fatal编程技术网

C# 基于外部属性的排序

C# 基于外部属性的排序,c#,linq,C#,Linq,我有一个现有的LINQ查询来检索一些项目: var result = from foo in x.SomeThings from bar in x.OtherThings where foo.RefId == bar.RefId select foo; x是一个包含三个属性的对象: 列出一些东西 列出其他内容 List MyStuffs包含一个也是虚构的属性 以下是这些课程的概述: public class X {

我有一个现有的LINQ查询来检索一些项目:

var result = from foo in x.SomeThings
             from bar in x.OtherThings
             where foo.RefId == bar.RefId
             select foo;
x是一个包含三个属性的对象:

  • 列出一些东西
  • 列出其他内容
  • List MyStuffs
    包含一个也是虚构的属性
  • 以下是这些课程的概述:

    public class X
    {
        public List<MyThing> SomeThings;
        public List<MyThing> OtherThings;
        public List<MyStuff> MyStuffs;
    }
    
    public class MyThing
    {
        public int RefId;
    }
    
    public class MyStuff
    {
        public MyThing TheThing;
        public DateTime WhenHappened;
    }
    
    公共类X
    {
    公开列出一些事情;
    公开列出其他事项;
    公开名单;
    }
    公共阶级神话
    {
    公共int RefId;
    }
    公共类MyStuff
    {
    公众对事物的虚构;
    出现时的公共日期时间;
    }
    
    如何根据匹配的RefId值,根据whenhapped的最早值对返回的foo进行排序?

    因此,您可以在此处使用
    Join
    操作符,而不是使用
    SelectMany
    进行完整的笛卡尔积(这是代码示例的最终结果),它的性能会更好

    接下来,为了按显示的值排序,您需要连接所有三个列表,而不仅仅是前两个列表。加入所有三个序列后,排序非常简单:

    var query = from first in x.SomeThings
                join second in x.OtherThings
                on first.RefId equals second.RefId
    
                join third in x.MyStuffs
                on first.RefId equals third.TheThing.RefId
    
                orderby third.WhenHappened
                select first;
    

    此查询的结果是,当出现
    MyStuffs

    属性时,它将返回按
    排序的所有三个序列中的项目。首先,如果集合较大,最好使用
    join
    <代码>来自x中的foo。对象在x中连接bar。foo上的对象。RefId等于bar。RefId select foo
    通常效率更高。这个问题有点让人困惑。你能给我们一个典型的例子,你能在“x”中找到什么样的数据吗?我在这里很困惑。
    事物的类型是什么?您想在此处加入的
    Baz
    对象是否有单独的列表?对不起。我试图澄清我的问题。如果仍然难以理解,请告诉我。看起来您还需要通过
    TheThing.RefId
    将其加入您的
    mystuff