Reputation: 9080
I have an object:
public class DataItem
{
public string Location
{
get;
set;
}
public List<string> PersonList
{
get;
set;
}
}
I have some results from a table that return something like:
Room1 John
Room1 Jim
Room1 Dawn
Room1 Bob
Room1 Katie
I have some LINQ that I've written:
var grouped = from table in sqlResults.AsEnumerable()
group table by new { placeCol = table["LOCATION"] } into groupby
select new
{
Value = groupby.Key,
ColumnValues = groupby
};
Which groups my results... But I'd like to put this into my object (DataItem). I've seen a couple of examples but nothing has worked... What am I missing?
Upvotes: 0
Views: 7334
Reputation: 3450
Since in your sample data (of DataItem's?) only includes one person in each Location, I'm questioning if the PersonList property on your DataItem class should really be just a single Person (or "string" in your example). It looks like you are querying a list of people and their location, and then trying to group those results by location and list each person in that location. I think you are trying to create something like a Lookup.
Look at this code.
public class DataItem
{
public string Location
{
get;
set;
}
public string Person
{
get;
set;
}
}
// omitted fetch of sqlResults
var grouped = sqlResults.AsEnumerable()
.ToLookup(
item => new
{
Location = item.Location
}),
item => new
{
Person = item.Person
});
Or maybe you are trying to stuff the results of the LINQ query into your original DataItem. If you are only accessing the grouped variable within the scope of the method, you can just use the inferred type. Otherwise, one of the other answers are correct.
Upvotes: 0
Reputation: 203829
Don't group on a new anonymous object with a single value representing your location, just group on the location
Don't select a new anonymous object as the result, select the object you care about
Select out the person name from the group when getting the person list.
var grouped = from row in sqlResults.AsEnumerable()
group row by row.Field<string>("LOCATION") into groupby
select new DataItem()
{
Location = groupby.Key,
PersonList = groupby.Select(row =>
row.Field<string>("Person")).ToList();
};
Upvotes: 3
Reputation: 3240
You only need the initialize a new instance of DataItem
, ie:
select new DataItem
{
Location = groupby.Key,
PersonList = groupby.ToList()
};
the idea of select
is to select / return an instance for each iteration of the expression so you just need the specify what you want to return. (When use only new
you're actually initializing a new anonymous type).
Upvotes: 0
Reputation: 62246
May be something like:
select new DataItem
{
Location= groupby.Key,
PersonList = groupby
};
Note, that in this case you have to declare PersonList
as IEnumerable<string>
Upvotes: 0
Reputation: 125630
Change your select clause. You also don't need to use anonymous object as a grouping key, because you use only one value as a key.
var grouped = from table in sqlResults.AsEnumerable()
group table by table["LOCATION"] into groupby
select new DataItem
{
Location = groupby.Key,
PersonList = groupby.ToList()
};
Upvotes: 0