0

I'm working on an ASP.Net application which uses a SQL-Server DB and database entities. Further i got three database entities which are dependend on each other. This is the dependency hierarchy:

  • Instance (Key: InstanceID)
    • CustomField (Key: CustomFieldID, InstanceID)
    • CustomFieldData (Keys: CustomFieldDataID, CustomFieldID)
      • CustomFieldData_Person (Keys: CustomFieldData_PersonID, CustomFieldDataID)

I can find out the entries from the entity CustomField by this with the InstanceID:

var customFieldEntries = DB_Instance_Singleton.getInstance.CustomField.Where(x => x.InstanceID == instanceId);

Now i want to find out all entries from CustomFieldData_Person which belong to the hierarchy with the InstanceID as key.

In SQL i would write something like this:

SELECT * FROM CustomFieldData_Person WHERE CustomFieldDataID in (
  SELECT * FROM CustomFieldData WHERE CustomFieldID in (
    SELECT * FROM CustomField WHERE InstanceID = instanceId))

Unfortunately i'm absolutely new to LINQ. So my question is, how can i write such a nested query in LINQ (aacording to the first code example above)?

Thanks in advance!

1

2 Answers 2

1

Firstly if you create your ER model correctly you will have most of that logic already set up for you

Person would have a property Person.CustomData which would have Properties for Field and Value so you can just navigate the object structure

however if you dont have that then you can just convert the in statements to Contains

CustomFieldData_Person.Where(cfdp=>CustomFieldData.Where(nested query for CustomFieldData).Contains(cfdp.CustomFieldDataID )
Sign up to request clarification or add additional context in comments.

Comments

0

I think this link could be a good starting point for your question. Anyway take a look at Pranav's comment, it points to a helpful question

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.