Ali Foroughi
Ali Foroughi

Reputation: 4629

Select Last Childs in a self-joined table in EF

I have a table like this :

Id              int      Not Null  PK Unique
RequestNO       int      Not Null   
RefrenceId      int      Null                  //Self Join to Id

If I have these records in this table :

Id   RequestNO    RefrenceId      
1       H100         NULL
2       H101         NULL
3       H101          2
4       H101          3
5       H100          1
6       H105         NULL

Depend on these records I want to return a list where Id is in { 4 ,5 , 6 }.I want to select last childrens (If any parents have not childs, itself is a child). Is there any one to help me about this ?!!

I am using EF 4 (Database First) , and the table name is Requests

UPDATE1 : I try this , but it failed.

var list = DataContext.Requests.GroupBy(rec => new { rec.ConfirmNo }).Select(rec => rec.FirstOrDefault());

If you need more details comment me. Thanks Ali Foroughi

Upvotes: 4

Views: 91

Answers (1)

NaveenBhat
NaveenBhat

Reputation: 3328

You can try this:

var list = DataContext.Requests.Where(op => !DataContext.Requests.Any(ip => ip.RefrenceId == ip.Id));

Upvotes: 1

Related Questions