0

I have List of DTO Suach as :

 Public Class UKey
{
    public Int64 Key{ get; set; }

}

Public Class Test : UKey
{
    public Int64? CityId  { get; set; }
    public Test2  test2{ get; set; }
}
Public Class Test2 : UKey
{
    public Int64? CountryId { get; set; }
    public Test3 test3 {get;set;}
}
public Class Test3 :UKey
{

}

I have nested DTOs,for example class test has a member of class test 2 and class test2 has a member of type class test 3,each class has it's own unique key and this key can not be repeated in any of them,somthing like GUid. I want to query Class Test to find just one of these nested Dtos with the given unique key.

M.Mohammadi
  • 1,558
  • 1
  • 13
  • 25

1 Answers1

0

Assuming tests object is IEnumerable, which is a set of Test objects;

tests.SingleOrDefault(q => q.test2.Key == id || q.test2.test3.Key == id);

UPDATE: you need to apply a recursive search. I have changed the base class a bit;

public class UKey
{
public Int64 Key { get; set; }
public UKey ReferencedEntity { get; set; }
}

and the search function:

private UKey Search(UKey entity, Int64 id)
{
    UKey result = null;
    if (entity.Key == id)
        result = entity;
    else
    {
        result = this.Search(entity.ReferencedEntity,id);
    }
    return result;
}
M.Mohammadi
  • 1,558
  • 1
  • 13
  • 25