score:43
Accepted answer
it sounds like you want something like:
// no need to sort sites first
var grouped = sites.orderby(x => x.type)
.groupby(x => x.type);
then just serialize grouped
. however, i don't know quite what an igrouping
will look like in json... and the type will be present in each case. you may want something like:
var grouped = sites.orderby(x => x.type)
.groupby(x => x.type)
.select(g => new { type = g.key,
sites = g.select(site => new {
site.title,
site.url
} });
i think that would give you a nicer json structure.
score:3
here's a straightforward console application that does what you want:
static void main(string[] args)
{
list<siteinfo> sites = new list<siteinfo>()
{
new siteinfo() { title = "site a", type = "whatever 2" },
new siteinfo() { title = "site b", type = "whatever 1" },
new siteinfo() { title = "site c", type = "whatever 1" },
new siteinfo() { title = "site d", type = "whatever 3" },
new siteinfo() { title = "site e", type = "whatever 3" }
};
var sitesgroupedbytype =
sites.groupby(s => s.type).select(g => new { type = g.key,
sites = g.select(site => new
{
site.title,
site.url
})});
foreach (var sitetypegroup in sitesgroupedbytype.orderby(g => g.type))
{
foreach(var site in sitetypegroup.sites)
{
console.writeline(string.format("type => {0}, title => {1}",
sitetypegroup.type, site.title));
}
}
console.readkey();
}
output:
type => whatever 1, title => site b
type => whatever 1, title => site c
type => whatever 2, title => site a
type => whatever 3, title => site d
type => whatever 3, title => site e
score:5
this
var sites = new list<siteinfo>()
{
new siteinfo(){title="1",type="a",url="http://aaaa"},
new siteinfo(){title="2",type="b",url="http://bbbb"},
new siteinfo(){title="3",type="a",url="http://aaaa"},
new siteinfo(){title="4",type="b",url="http://bbb"},
};
var json = new javascriptserializer().serialize(sites.groupby(s => s.type));
would produce
[
[{"title":"1","url":"http://aaaa","type":"a"},{"title":"3","url":"http://aaaa","type":"a"}],
[{"title":"2","url":"http://bbbb","type":"b"},{"title":"4","url":"http://bbb","type":"b"}]
]
or
var json = new javascriptserializer().serialize(sites.groupby(s => s.type)
.todictionary(x=>x.key,x=>x));
would produce
{
"a":[{"title":"1","url":"http://aaaa","type":"a"},{"title":"3","url":"http://aaaa","type":"a"}],
"b":[{"title":"2","url":"http://bbbb","type":"b"},{"title":"4","url":"http://bbb","type":"b"}]
}
Source: stackoverflow.com
Related Query
- Group List of Objects based on Property using Linq?
- Using LINQ to get the difference between two list of objects based only on a single object property
- Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate
- Select objects from a list using LINQ based on a property
- How do i group a list of dynamic objects using linq given that the property names are dynamic?
- Group objects in List by property in object that is List using LINQ
- Using Linq to group a list of objects into a new grouped list of list of objects
- Using LINQ to group a list of objects
- Change the property of objects in a List using LINQ
- Group items in a list based on two properties using LINQ
- Linq - group a list into pairs based on a property
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- LINQ query using list of objects property of an object
- Get the objects with the maximum value for a specific property from a list using LINQ
- Using LINQ to group a list of strings based on known substrings that they will contain
- select and update based on a child objects property minimum value using Linq
- Filter a list of objects using a property that is another list. Using linq
- sorting list of objects by 2 property of that objects using linq
- Filter list of entity objects by string child object property using Linq Lambda
- Join a string property which is in a List of objects using Linq
- Group list of objects with corresponding child with multiple inheritance using LINQ
- Using a Linq query to select objects where any value of a property matches values from a list
- Using Linq to Filter a List of Objects based on a Condition
- Using linq to sort list of objects with string property
- Group a list of anonymous type objects using linq and lambda
- Using Linq to group a list of objects into a new grouped list of list of objects with 4 objects in each group
- Using linQ to group list of object into new lists of objects
- Update a property on objects in a list and add it to another list without editing the original list using LINQ
- Group items in a list based on two properties using LINQ MVC
- Using Linq to group objects that contain a list field
More Query from same tag
- The ':' character, hexadecimal value 0x3A, cannot be included in a name
- How to connect to SQL Server Express DB from Server Explorer VS 2010?
- LINQ query to find if all items in list 2 are contained in list 1
- Convert this SQL Count statement to LINQ statement
- what LINQ query will return results based on partial matches against arbitrary number of an entity's fields?
- LINQ Nested Where
- dynamic linq expression using select in .NET
- Create key for cache
- How to replace "-" with empty space when columns are null in LINQ
- CS0176 Compiler error. What does it mean an how do I solve it
- How to merge more than one list having objects into one list with sum of one of the elements of the list in c#
- Linq Expression snaphot an index value
- Filling a property of list from another property of list using lambda?
- Using LINQ Lambda expression determining value by group by and where condition
- How to search string on list
- Grouping DataTable in C# and add duplicated rows in new columns using Linq
- Linq c# use between on time span
- How to write LINQ query for fetching the specific records and generating new result at the same time?
- Is it possible to .SkipWhile(n => (function)) in linq where (function) looks at the next x rows?
- Date falls between two dates and matches one date from table in Linq
- LINQ EntityFramework, sort by the attributes
- Combining expression trees
- Check last character is "&" is not and remove it (make this with linq)
- Not able to filter null value for integer column in DataTable
- Updating local iqueryable list before returning to method call
- How to find the last item from list have the same item using linq
- c# create method that accept lambda for list of field name
- Linq Join Duplicates records c#
- Show LINQ joined table column at view DisplayNameFor
- Excluding lists