score:1
after thinking about this, this is what i came up with. it's more or less the same as kirk's but doesn't use a dictionary. i was aiming for a single query to do this, (select ... into) really saved me there.
var query = from item in data
where item.rowlabel == "all"
orderby item.value descending
select item.columnlabel into columnorder
join item in data on columnorder equals item.columnlabel
group item by item.rowlabel;
var lquery = data.where(item => item.rowlabel == "all")
.orderbydescending(item => item.value)
.select(item => item.columnlabel)
.join(data, columnorder => columnorder,
item => item.columnlabel,
(columnorder, item) => item)
.groupby(item => item.rowlabel);
score:2
i believe this will do what you want:
var order = data
.where(x => x.rowlabel == "all")
.orderbydescending(x => x.value)
.select((x, i) => new { x, i })
.todictionary(x => x.x.columnlabel, x => x.i);
var result = data
.groupby(x => x.rowlabel, (x, y) => y.orderby(z => order[z.columnlabel]));
foreach (var x in result)
{
foreach (var y in x)
{
console.writeline("columnlabel: {0}, rowlabel: {1}, value: {2}", y.columnlabel, y.rowlabel, y.value);
}
}
result:
columnlabel: dog, rowlabel: all, value: 17
columnlabel: cat, rowlabel: all, value: 9
columnlabel: fish, rowlabel: all, value: 4
columnlabel: dog, rowlabel: girls, value: 2
columnlabel: cat, rowlabel: girls, value: 3
columnlabel: fish, rowlabel: girls, value: 2
columnlabel: dog, rowlabel: boys, value: 15
columnlabel: cat, rowlabel: boys, value: 6
columnlabel: fish, rowlabel: boys, value: 2
let me know if i misinterpreted your question, but the idea here is to create a dictionary order
that provides the ordering for the subsequent group-by query. the other key idea is to use the resultselector of group-by to order the elements within (since those represent the columns).
Source: stackoverflow.com
Related Query
- linq - how do you do a query for items in one query source that are not in another one?
- Another LINQ OrderBy query
- LINQ Orderby Descending Query
- LINQ query to find if items in a list are contained in another list
- How to OrderBy an integer in a string field in a Linq query
- How to apply multiple orderby in linq query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- LINQ - writing a query with distinct and orderby
- LINQ query adding orderby makes Skip and Take not work Linqpad
- Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another
- Linq query join one table row to another table multiple row
- LINQ Query - How to map a resultset into another object using Select
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Paginating a linq query which uses OrderBy
- Linq query with multiple OrderBy statements added in a loop
- LINQ Source Code Available
- multiple orderby in this linq code
- Linq query - List within another list
- Which LINQ query to select rows from 1 table that are not in another table
- filter a linq query based on the results of another query's results
- LINQ query OrderBy doesn't work
- How can I write the following code more elegantly using LINQ query syntax?
- How to use LINQ to query list of strings that do not contain substring entries from another list
- LINQ query for finding one item in list AND verifying list does not contain another item
- Linq query with OrderBy word count
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- C# Linq query help removing foreach loops creating cleaner code
- Use a linq query as microsoft local report Data Source (WinForms)
- LINQ Lambda efficiency of code groupby orderby
More Query from same tag
- Value was either too large or too small for an Int16 ,when copy to data table
- Full outer join, on 2 data tables, with a list of columns
- Why is LINQ non-deterministic?
- How to use LINQ to retrieve a list from another entity with a specific number of items
- How to add index with page numbers using itextshap?
- Comparing lists of integers and then ordering them based on similarity.
- Intersect not giving expected results
- Using Linq how I have to add list items
- How to loop within a Linq to XML statement
- Linq contain query in two fields
- How can I add Dictionary Object dynamically in LINQ?
- How can i convert this query to Linq?
- C# find xml element matching two element levels using linq
- Create a list from another list
- how do I make this LINQ query faster?
- SPMetal: How to override it to use internal name instead of “Display Name”
- XDocument is removing document comment
- Is Order of items read from a XDocument, by LINQ, Guaranteed?
- Looping on IEnumerator<T>, Any Suggestions
- Is a linq query to ConcurrentDictionary Values threadsafe?
- Loop hierarchy to root
- Use outer join or wrap second context call in where clause?
- How do I perform a linq GroupBy across 3+ columns
- Linq Contains without considering accents
- How do you create a dynamic select projection from a generic using Linq in C#?
- Writing a predicate for Linq (2Sqlite)
- Using LINQ to group on ID, EmailListID, PhoneListID, etc
- How to retrieve more than one instance of a unique Id with linq?
- Many to Many in Linq using Dapper
- Most Ordered Products via Linq