gpt4 book ai didi

c# - 按 LINQ 中的 3 个子字符串组成的字段排序

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:20 25 4
gpt4 key购买 nike

我有一个如下所示的 LINQ 查询,

 var sortedResults = (from r in this.CurrentTemplateInfos
where r.EffectiveEnd == null
orderby r.Description
select r);

从上面的查询中,字段描述由3个子字符串(Showtype:Show:StartDate-Endate)组成,用冒号(:)分隔。现在根据新要求,i 需要对前两个 fields(Showtype:Show) 进行升序排序,然后根据描述值按 StartDate 降序,例如

Show : Main Street Trolley : 6/12/2010 - 7/15/2010

ShowType : Parades : 2/10/2010 - 6/16/2010

ShowType : Parades : 6/17/2010 - 8/26/2010

ShowType : Parades : 8/27/2010 - 10/26/2010

输出应该是

Show : Main Street Trolley : 6/12/2010 - 7/15/2010

ShowType :Parades : 8/27/2010 - 10/26/2010

ShowType : Parades : 6/17/2010 - 8/26/2010

ShowType : Parades : 2/10/2010 - 6/16/2010

请帮我解决这个问题。

最佳答案

将字段分解为具有辅助类型的单独字段,然后按子句顺序使用它。像这样的东西:

// Elsewhere
class SortFields {
public string Field1 { get; set; }
public string Field2 { get; set; }
public DateTime StartDate { get; set; }

public static SortFields SplitInput(string input) {
// Factory... Implement split/parse logic
}
}

然后

from r in this.CurrentTemplateInfos
where r.EffectiveEnd == null
let fields = SortField.SplitInput(r.Description)
// This means sort by Field1, then Field2 and then StartDate
// and the default in each criterion is ascending unless you
// specify descending keyword.
orderby fields.Field1, fields.Field2, fields.StartDate descending
select r

请注意,可以使用多个 let 子句来进行拆分/解析,但我认为 little helper 类型更清晰。

关于c# - 按 LINQ 中的 3 个子字符串组成的字段排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com