There are two tables: CLASSIFICATION contains Name, Main Class and QUESTION contains Question and Classification Id

Formed a hierarchy of classes

Need to create a linq query where will the classifications be displayed that have questions or have questions in their subclasses?

  • 7
    Are you using Entity Framework or what? And give the code of the mentioned classes in the question, otherwise nothing is clear. - Andrey NOP
  • The main class - what is this field? ParentId? - AK

1 answer 1

Implement this interface in your parent class of the required classification and you will receive classifications that have questions or have questions in their subclasses.

public class Foo { public Foo() { List<Сlassification> classfications = new List<Сlassification>(); List<Question> questions = new List<Question>(); questions.Add(new Question() { TextQuestion = "NotEmpty1" }); questions.Add(new Question() { TextQuestion = "NotEmpty2" }); questions.Add(new Question() { TextQuestion = "NotEmpty3" }); questions.Add(new Question() { TextQuestion = string.Empty }); questions.Add(new Question() { TextQuestion = "NotEmpty5" }); List<IClassificationMy> result = questions.Where(f=> f.IsCorrect()).Select(f => f.MyClassification).ToList(); Console.WriteLine(result.Count()); } } public class Сlassification : IClassificationMy { public Сlassification() { } public object MainClass { get; set; } public string Name { get; set; } } public class Question { public Question() { MyClassification = new Сlassification(); } public string TextQuestion { get; set; } public IClassificationMy MyClassification { get; set; } public bool IsCorrect() { return this.TextQuestion != string.Empty; } }