简体   繁体   English

Linq OrderBy属性值和组

[英]Linq OrderBy Attribute Value and Group

I basically have a table with headers that I read in from a DB using linq in C#. 我基本上有一个带有标题的表,我在C#中使用linq从数据库中读取。 Of these headers, there is always at least one that is always the same; 在这些标题中,始终至少有一个始终相同; Total and I want it to always be on the right. 总计和我希望它始终在右边。

So here is sort of how my data is laid out: 所以这里有一些我的数据如何布局:

Data
{
   Label,
   Value,
   Age  //Not used initially
}

Sample Data: 样本数据:

{"Dog", 7}
{"Cat", 3}
{"Other", 4}
{"Total", 14}

I'd like to order the labels in this order; 我想按此顺序订购标签; the actual animal names are sorted by their value in descending order and Total is appended to the end: 实际的动物名称按其值按降序排序,Total附加到结尾:

"Dog", "Other", "Cat", "Total"

How do I do this in Linq. 我如何在Linq中执行此操作。 How do I order an attribute based upon the value of another attribute? 如何根据另一个属性的值订购属性?

Once I have the order of the headers, is there an easy way to order future rows based upon the already determined order. 一旦我有了标题的顺序,是否有一种简单的方法可以根据已经确定的顺序订购未来的行。 If I want to initially find headers where(x=>x.Age > 20) how can I sort the Labels in where(x=>x.Age <= 20) based upon the same ordering as the >20 set? 如果我想最初找到其中的标题(x => x.Age> 20),我如何根据与> 20集相同的顺序对其中的标签进行排序(x => x.Age <= 20)?

This query should work: 此查询应该有效:

var query = from row in table
            let ignore = row.Label == "Total"
            orderby ignore, row.Value descending
            select row.Label;

//and corresponding lambda version
var lquery = table.OrderBy(row => row.Label == "Total")
                  .ThenByDescending(row => row.Value)
                  .Select(row => row.Label);

Note: it isn't entirely necessary to create a ignore variable, it could be placed directly in the orderby clause. 注意:创建一个ignore变量并不是完全必要的,它可以直接放在orderby子句中。 This way, it makes it more readable. 这样,它使它更具可读性。

Not sure what you are asking in your second question. 在第二个问题中不确定你在问什么。


edit: 编辑:
In response to your comment, it would depend on how you wanted the sorting to work, at least written like this. 在回应你的评论时,这将取决于你希望排序如何工作,至少写得这样。 This works well if there is only one row in the table that you want to completely ignore. 如果表中只有一行要完全忽略,则此方法很有效。 Not so much if you had more than one. 如果你不止一个,那就不那么重要了。 The problem being that among the "ignored" rows, will be sorted by the original sorting as well (in this case, by Value ). 问题是在“被忽略”的行中,也将按原始排序(在本例中为Value )进行排序。 A naive way to add another row is to add to the ignore condition. 添加另一行的简单方法是添加到ignore条件。

var query = from row in table
            let ignore = row.Label == "Total" || row.Label == "Cost"
            orderby ignore, row.Value descending
            select row.Label;

To have a specific ordering among the "ignored" rows, it would require a somewhat more complex query: 要在“忽略”行中进行特定排序,需要更复杂的查询:

var query = from row in table
            let ignore = row.Label == "Total" || row.Label == "Cost"
            let ignoreorder = row.Label == "Cost" ? 1 : 0
            orderby ignore, ignoreorder, row.Value descending
            select row.Label;

//and corresponding lambda version
var lquery = table.OrderBy(row => row.Label == "Total" || row.Label == "Cost")
                  .ThenBy(row => row.Label == "Cost" ? 1 : 0)
                  .ThenByDescending(row => row.Value)
                  .Select(row => row.Label);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM