Reputation: 260
I need to return a subset of an object based off of a list of values in another object.
Object to return:
public class Item
{
public string Title {get;set;}
public Item subItems {get;set;}
}
I want to select a subset of "Items" based off of the items property on a User table:
public class User
{
public string ID {get;set;}
public Subscription[] subscriptions {get;set;}
}
public class Subscription
{
public string Title {get;set;}
}
So, each User will have an array of subscriptions. I want to return an Item object for a specific user, where the Item Title is in the list of Titles within the user's subscription.
eg...
Items:[
{ title: "USA Today",
otherdata: "..."},
{ title: "WSJ",
otherdata: "..."},
{ title: "ET Weekly",
otherdata: "..."}
]
Users: [
{ ID: "joe schmo",
subscriptions" : [
{ item : "USA Today" },
{ item : "ET Weekly" }
]
}...]
In this example, I want to return :
items = [ {title : "USA Today"
otherdata: "..."},
{title: "ET Weekly",
otherdata: "..."}
]
Upvotes: 0
Views: 1518
Reputation: 203837
What you have here is just two operations, one to flatten the sequence of users into the sequence of subscriptsions, and then a Join of the subscriptions with items on the Title
property of each:
var query = from user in users
from subscription in user.subscriptions
join item in items
on subscription.Title equals item.Title
select item;
Upvotes: 2