Lambda Statement that selects all child that match criteria - vb.net

I have a data structure as listed below. How do I get the children of the children that meet the criteria?
I have a list of Adults that contain a list of children.
AdultJoe
ChildJoe Age 12
ChildJane Age 10
AdultFrancis
ChildTom Age 12
ChildTony Age 10
Using the above data structure how do I get all of the child that are age 12?
This is what I've tried and it's not doing what I need.
Adults.Where(Function(adult) adult.Children.Any(Function(child) child.age= 12))

To "flatten" an IEnumerable of IEnumerables, use SelectMany:
Dim children = Adults.SelectMany(Function(a) a.Children).Where(Function(c) c.Age = 12)
This is implicit in the LINQ syntax:
Dim children = from adult in Adults
from child in adult.Children
where child.Age = 12
select child

You didn't describe your classes, but assuming that the adults have a Children property, you could use something like this:
Dim twelveYearOlds = adults.SelectMany(
Function(adult) adult.Children
).Where(
Function(child) child.Age = 12
)
The SelectMany gives you a list of all the children of all the adults, and the where filters that list to the 12-year-olds.

Related

Rails, joining two tables with where clauses on each tabe

I'm new to web development and rails, and I'm trying to construct a query object for my first time. I have a table Players, and a table DefensiveStats, which has a foriegn-key player_id, so each row in this table belongs to a player. Players have a field api_player_number, which is an id used by a 3rd party that I'm referencing. A DefensiveStats object has two fields that are relevant for this query - a season_number integer and a week_number integer. What I'd like to do is build a single query that takes 3 parameters: an api_player_number, season_number, and week_number, and it should return the DefensiveStats object with the corresponding season and week numbers, that belongs to the player with api_player_number = passed in api_player_number.
Here is what I have attempted:
class DefensiveStatsWeekInSeasonQuery
def initialize(season_number, week_number, api_player_number)
#season_number = season_number
#week_number = week_number
#api_player_number = api_player_number
end
# data method always returns an object or list of object, not a relation
def data
defensive_stats = Player.where(api_player_number: #api_player_number)
.joins(:defensive_stats)
.where(season_number:#season_number, week_number: #week_number)
if defensive_stats.nil?
defensive_stats = DefensiveStats.new
end
defensive_stats
end
end
However, this does not work, as it performs the second where clause on the Player class, and not the DefensiveStats class -> specifically, "SQLite3::SQLException: no such column: players.season_number"
How can I construct this query? Thank you!!!
Player.joins(:defensive_stats).where(players: {api_player_number: #api_player_number}, defensive_stats: {season_number: #season_number, week_number: #week_number})
OR
Player.joins(:defensive_stats).where("players.api_player_number = ? and defensive_stats.season_number = ? and defensive_stats.week_number = ?", #api_player_number, #season_number, #week_number)

How to limit subnodes from each nodes Neo4j Cypher

I am new to Neo4j,I have the following situation
In the above diagram represented a node with label user with sub-nodes having label shops. Each of these sub-nodes have sub-nodes with label items. Each node items has attribute size and the items node is in descending order by size attribute for each node shops as represented in the figure.
Question
I want to get two items node whose size is less than or equal to 17 from each shops .
How to do that? I tried, but its not working the way I need
Here is what I have tried
match (a:user{id:20000})-[:follows]-(b:shops)
with b
match (b)-[:next*]->(c:items)
where c.size<=17
return b
limit 2
Note- These shops node can have thousands of items nodes. So how to find the desired nodes without traversing all thousands of items nodes.
Please help , thanks in advance.
Right now Cypher does not handle this case well enough, I would probably do a java based unmanaged extension for this.
It would look like this:
public List<Node> findItems(Node shop, int size, int count) {
List<Node> results=new ArrayList<>(count);
Node item = shop.getSingleRelationship(OUTGOING, "next").getEndNode();
while (item.getProperty("size") > size && results.size() < count) {
if (item.getProperty("size") <= size) result.add(item);
item = item.getSingleRelationship(OUTGOING, "next").getEndNode();
}
return result;
}
List<Node> results=new ArrayList<>(count*10);
for (Relationship rel = user.getRelationships(OUTGOING,"follows")) {
Node shop = rel.getEndNode();
results.addAll(findItems(shop,size,count));
}
You can avoid having to traverse all items of each shop by grouping them according to size. In this approach, your graph looks like this
(:User)-[:follows]-(:Shop)-[:sells]-(:Category {size: 17})-[:next]-(:Item)
You could then find two items per shop using
match (a:User {id: 20000})-[:follows]-(s:Shop)-[:sells]-(c:Category)
where c.size <= 17
with *
match p = (c)-[:next*0..2]-()
with s, collect(tail(nodes(p))) AS allCatItems
return s, reduce(shopItems=allCatItems[..0], catItems in allCatItems | shopItems + catItems)[..2]
shopItems=allCatItems[..0] is a workaround for a type checking problem, this essentially initializes shopItems to be an empty Collection of nodes.

bigquery query repeat field

I am learning the bigquery with the example: personsData
https://developers.google.com/bigquery/docs/data#nested
in this example, there is a repeated filed called children. Is this possible to make a query to find out who have child called "Josh" and child call "Jim"
I tried the following query but no result return:
select fullName from [xxx.xx] where children.name = "Jane" and children.name = "John"
A couple of things are wrong with the above query.
There should be an or not an and between the two criteria. e.g. children.name = "Josh" or children.name = "Jim"
If you are using the standard google data set then there is no children with the name "Josh" or "Jim"
try:
select fullName from PersonsData.personsdata where children.name = "Jane" or children.name = "John"
with google datasets

Certain size from collection, including second filter with HQL?

I'm struggling with following scenario: Let's assume I have the two entities Classroom and Member, mapped with many-to-many. Classroom has the collection Members, containing the entities Member.
I would like to get the classrooms which do have members of a certain count. That would result in something like:
FROM Classroom cr WHERE cr.Members.size < 10
Now I have a Type on Classroom. I'd like to filter first on Type, then the size. This won't work:
FROM Classroom cr WHERE cr.Members.size < 10 AND cr.Members.Type = 1
Results in: illegal attempt to dereference collection
How could I write such a query?
I would imagine you need to do a join
from Classroom as cr left join cr.Members as m
where cr.Members.size < 10 and m.Type = 1

Nhibernate partial eager load of child collection

If I have a parent object (Parent) which has a List(Of Child) objects as a many-one relationship. Is it possible to return a Parent with a subset of it's child objects (eagerly loaded)? I am using VB and Criteria.
e.g. If Parent 1 has 50 children (20 type X 30 type Y) and I want to return the Parent with a collection containing only type X.
I only want a collection with a size of 20 with it's eagerly loaded children?
Thanks
HQL query. The fetch keyword will initialize the children along with the parent.
from parent left join fetch parent.Children as child where child.type = X