How to write this linq query?
I have a first query that returns a set of entities:
var resultSet = ....query....ToList();
which would return A, B, C, D, E
The entities inside this set are organized into chains because they have a reference (prevEntityId) pointing to the same type of entity, i.e.:
A -> B -> D
C -> E
I would like to write a second query so that only A and C are now returned but I have no idea how to write it.
I would prefer your answer with linq methods (like .Where()) instead of the new linq syntax.
Thank you
Update: sorry I initially used the wrong vocabulary in my question with the term "foreign key". Actually an entity has a direct reference to the previous entity so to elect an entity in the second query, there must be no other entity that references it. I thought it w开发者_运维知识库ould take 2 queries but if you think it can be done in one...
EDIT: Okay, as the relationship is the opposite of what I expected, you're looking for entities such that no entity has it as the previous one.
One option:
Where(x => !fullList.Any(y => y.PrevEntityId == x.Id));
Now, if your query is actually being executed in SQL it would be worth seeing what that generates - it may well not be efficient. It pretty definitely won't be efficient in LINQ to Objects. For LINQ to Objects, you'd be better off with:
HashSet<string> prevIds = new HashSet<string>(fullList.Select(x => x.PrevId));
var heads = fullList.Where(x => !prevIds.Contains(x));
If you could more information about your environment, that would help us to help you.
I am not 100% clear on your entities and their relationships, but this will get you started.
It sounds like you're trying now to further query from your resultSet
object. If wanting A and C, then it sounds like the FK would be null.
var cEntities = resultSet.Where(e=>e.prevEntityID == null).ToList();
You could write a helper function that given the last entity in a chain gives you the first entity. Then you can combine that with pcampbell's answer to get the first entities in a chain. Something along these lines
resultSet.Where(e=>e.prevEntityID == null).Select(e => GetHead(e, resultSet)).ToList();
Entity GetHead(Entity current, List<Entity> entities)
{
Entity next = entities.Where(e => e.prevEntityID = current.id).FirstOrDefault();
if (next == null)
return current;
else
return GetHead(next, entities);
}
精彩评论