Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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#List.OrderBy_C#_List_Linq - Fatal编程技术网

具有多个列表的C#List.OrderBy

具有多个列表的C#List.OrderBy,c#,list,linq,C#,List,Linq,我有5张名单。一个包含发布日期,另一个是该列表的属性,但在多个列表中分开 List-sortedDateList=x1.OrderBy(x=>x.ToList() 此代码是先对最早日期的列表进行排序,就像应该的那样。但我还想对其他属性列表进行排序(同步),因为它们需要与日期相同的索引 我怎么能意识到这一点?我不熟悉Linq方法。您可以使用.Zip()方法来组合所描述的列表。您可以将它们组合成类或匿名类型,然后对它们进行排序 int[] numbers = { 1, 2, 3, 4 }; stri

我有5张名单。一个包含发布日期,另一个是该列表的属性,但在多个列表中分开

List-sortedDateList=x1.OrderBy(x=>x.ToList()

此代码是先对最早日期的列表进行排序,就像应该的那样。但我还想对其他属性列表进行排序(同步),因为它们需要与日期相同的索引

我怎么能意识到这一点?我不熟悉Linq方法。

您可以使用.Zip()方法来组合所描述的列表。您可以将它们组合成类或匿名类型,然后对它们进行排序

int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };

var numbersAndWords = numbers.Zip(words, (first, second) => new { Num = first, Word = second });

var sorted = numbersAndWords.OrderBy(x => x.Num).ToList();
或者,如果可以保证所有列表的长度相同(或者只获取最短的列表),则可以使用以下扩展名,而不是.Zip()扩展名

var numbersAndWords = numbers.Select((number, i) => new { Num = number, Word = words[i], Foo = myFoos[i] }); // Where myFoos is another collection.

在lambda中,通过索引访问集合,同时将单独列表中的所有项合并到一个对象中。(避免多次使用.Zip())当然,如果您尝试访问大于列表大小的索引,您将得到一个
索引AutoFrangeException

据我所知,您有不同的包含特定对象属性的列表。您肯定应该考虑将所有数据存储到您制作的类的一个列表中,将所有单独的信息合并到一个对象中:

var list = new List<YourClass>
{
    new YourClass
    {
        Date = ...,
        OtherProperty = ...,
    },
    new YourClass
    {
        Date = ...,
        OtherProperty = ...,
    },
};

var ordered = list.OrderBy(o => o.Date);

然后,您可以使用已排序对象的索引在其他列表中按索引查找属性,或按中所述的索引对属性进行排序,依此类推。

听起来您几乎想要一个类的列表

public class MyClass{
    public string Date{get; set;} //DateTime is a better type to use for dates by the way
    public string Value2{get; set;}
    public string Value3{get; set;}
    public string Value4{get; set;}
    public string Value5{get; set;}
}

...

var sortedDateList = x1.OrderBy(x => x.Date).ToList()

创建包含日期和属性的对象:

public class DateWithAttributes
{
  public string Date {get;set;}
  public Attribute Attribute1 {get;set;}
  public Attribute Attribute2 {get;set;}
...
}

List<DateWithAttributes> DateWithAttributesList = new List<DateWithAttributes>()
{
  DateWithAttribute1,
  DateWithAttribute2
}
List<DateWithAttributes> sortedDateList = DateWithAttributesList.OrderBy(x => x.date).ToList();
公共类DateWithAttributes
{
公共字符串日期{get;set;}
公共属性Attribute1{get;set;}
公共属性Attribute2{get;set;}
...
}
列表日期WithAttributesList=新列表()
{
DateWithAttribute1,
DateWithAttribute2
}
List-SortedDataList=DateWithAttributesList.OrderBy(x=>x.date.ToList();

如果要将列表分开,和/或创建作为单独列表的有序版本,则可以将索引连接到日期并按日期排序,然后使用排序后的索引:

var orderedIndexedDateOfReleases = dateOfReleases.Select((d, i) => new { d, i }).OrderBy(di => di.d);

var orderedDateOfReleases = orderedIndexedDateOfReleases.Select(di => di.d).ToList();
var orderedMovieNames = orderedIndexedDateOfReleases.Select(di => movieNames[di.i]).ToList();
如果不介意合并结果,可以创建一个类或使用匿名类,然后再次按日期排序:

var orderedTogether = dateOfReleases.Select((d, i) => new { dateOfRelease = d, movieName = movieNames[i] }).OrderBy(g => g.dateOfRelease).ToList();

其他列表是如何定义的?为什么不存储一个包含属性的对象列表,而不是每个属性的列表?您可以在调用
.ToList()
之前添加
.ThenBy()
方法。我非常喜欢Linq,但如果您在x1列表中添加一个新方法,可能会更容易。SortAll或其他在内部处理你的方法的东西。所以你可以叫x1.SortAll。在内部,你可以一个接一个地订购。是的,但他们有五张单子。当然,您可以将它们全部压缩,但这将很快成为非常笨拙的语法。@CodeCaster+1我同意,这就是为什么我提出了按索引访问的建议。我看你也有同样的想法!;)
var orderedTogether = dateOfReleases.Select((d, i) => new { dateOfRelease = d, movieName = movieNames[i] }).OrderBy(g => g.dateOfRelease).ToList();