score:2
Accepted answer
You can create House objects in your select statement. The code below creates a list of House objects each containing the appropriate names:
class Program
{
static void Main(string[] args)
{
List<KeyValuePair<int, int>> housePersonPairs = new List<KeyValuePair<int, int>>();
housePersonPairs.Add(new KeyValuePair<int, int>(1, 11));
housePersonPairs.Add(new KeyValuePair<int, int>(1, 12));
housePersonPairs.Add(new KeyValuePair<int, int>(1, 13));
housePersonPairs.Add(new KeyValuePair<int, int>(2, 232));
housePersonPairs.Add(new KeyValuePair<int, int>(2, 5533));
housePersonPairs.Add(new KeyValuePair<int, int>(2, 40));
List<Person> persons = new List<Person>();
persons.Add(new Person() { ID = 11, Name = "John" });
persons.Add(new Person() { ID = 12, Name = "Jane" });
persons.Add(new Person() { ID = 13, Name = "Zoe" });
persons.Add(new Person() { ID = 232, Name = "Name1" });
persons.Add(new Person() { ID = 5533, Name = "Name2" });
persons.Add(new Person() { ID = 40, Name = "Name3" });
var houseAndNames = housePersonPairs.Join(
persons,
hpp => hpp.Value,
p => p.ID,
(hpp, p) => new { HouseID = hpp.Key, Name = p.Name });
var groupedNames = from hn in houseAndNames
group hn by hn.HouseID into groupOfNames
select groupOfNames.Select(x => x.Name).ToList();
List<House> houses = groupedNames.Select(names => new House() { people_name = names }).ToList();
}
}
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
}
public class House
{
public List<string> people_name { get; set; }
}
Source: stackoverflow.com
Related Query
- Create a list nested within an object in a LINQ query
- nested LINQ query - list within list
- Linq code to get the index of an object in an array from an object within a list
- LINQ query to create a list of objects where each object contains a list
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Linq Query to get List of Objects and for Each of Object get another nested List
- using linq and c# to create a nested list object from a flat list object
- Dynamic Linq Expression Query Nested List Object
- Linq query to object with nested list
- Create nested object on Entity Framework Linq query
- Group By object within a nested list linq
- Create a list from two object lists with linq
- Create a list of one object type from a list of another using Linq
- Linq query a List of objects containing a list of object
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- Nested LINQ query to select 'previous' value in a list
- Linq query - List within another list
- How to create a list from filtering 2 lists with linq to object
- LINQ query on an object list to get distribution based on multiple fields
- Return Linq query results into List object
- How to create a string[] within a LINQ query via Regex.Split()
- LINQ Query to put data in a nested object
- I am trying create a new list of users from a list of users who have not created an additional object with a certain property using LINQ
- LINQ query returns old results when source list is re-initialized
- LINQ query using list of objects property of an object
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- Filtering LINQ Query against member object list
- Create null enumerable within Linq to Entities query
- linq query to return nested list for unique items
More Query from same tag
- Linq query where there's a certain desired relationship between items in the result
- Issue using Where method in LINQ
- Grouping By Month from String Date using LINQ
- How to sort a list with the values of another list with Linq
- Fetch data From Two tables using Linq
- Visual Studio Code Analysis Rule - "Do not expose generic lists"
- How to delete in linq?
- Querying a list to find the count of an ID and merge the contents of two similar IDs
- Returning list of properties from a list of objects
- Perform Aggregate by grouping in Object list using LINQ
- Union collections only if second TSource is not Null
- EntityFramework with WEB API, update all properties
- Select data excluding a subset of records with LINQ with minimal overhead
- Linq to XML - Extract Single Element
- Use LINQ to Total Columns
- How to get results from another query list if first linq query returns empty?
- Querying dynamic data
- LINQ - Summing numbers stored as string
- Counting percent of rows with category out of total number of rows using Dynamic Linq
- Insert data into SQL Server CE for windows phone
- async/await inside LINQ where clause not working
- IEnumerable add item?
- LINQ to intersect elements of complex List<T> into new List<T>
- linq query to select top 3 from a group and get the average of those selected
- Why does this LINQ query assign a value of 1 to a NULL value from the database?
- C# Syntax From String
- The result of a query returns the same value for COLUMN_NAME from an Entity Framework query, which doesn't make sense
- Assigning values inside a LINQ Select?
- dot notation equivalent for JOIN
- Trying to get all elements after first match using linq