none
Parent Child entity RRS feed

  • Question

  • Hello,

    I have a parent child entity that represents a folder. each folder has a foreign key to it's parent folder.

    I get Folder tree using the Intersect method :

    IQueryable<Folder> foldersQuery1 =
                        from folders in aquaEntities.Folders.Include("ChildFolders")
                        select folders;
                    //var temp1 = foldersQuery1.ToList().Where(a => a.ParentFolder == null).Intersect(foldersQuery1);
                    return foldersQuery1.Where(a => a.owner==owner).ToList().Intersect(foldersQuery1).ToList();

    This works fine.

    But i need to write another method that receives a folderName (or part of the name) and gets all folders containing this name . The folder tree is built from the "root" folder (a root folder is a folder which it's parent Folder is NULL). How can i do it? Currently i can retrieve the folder (folder name is the one i am searching) and it's children and so on. But also i need this folders parent folder and so on untill the root folder.

    Tuesday, August 21, 2012 9:06 AM

All replies

  • Hi michaelgr123;

    Line to Entity Framework does not have support for the TSQL keyword LIKE which is what you are looking for. Entity Framework has another query language that does support what you need. That language is called Entity SQL, eSQL. The following code snippet uses the Northwind sample database to show an example of querying the Customer database the City field.

    // Code assumes you are using a DbContext
    
    // TextBox holds city name or part of name
    string cityPart = textBox1.Text.Trim( );
    // This is the eSQL command that will be sent note that the FROM clause is ContextName.TableName
    // There are other differences see documentation
    string esqlCmd = "SELECT VALUE c FROM NorthwindEntities.Customers AS c WHERE c.City LIKE '%" + cityPart + "%'";
    // Create the DbContext
    var dbContext = new NorthwindEntities( );
    
    // You need to get access to the underlaying ObjectContext
    var objectContext = ( ( IObjectContextAdapter ) dbContext ).ObjectContext;
    // Create the Entity Framework query
    var query = new ObjectQuery<Customer>( esqlCmd, objectContext );
    // Execute the query
    var result = query.Execute( MergeOption.AppendOnly );

    Entity SQL Overview

    Entity SQL Reference

    Entity SQL Language

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, August 21, 2012 4:35 PM
  • That's not my problem.

    I can do what you suggest using Contains.

    My Problem is that i need to get the Root Folder for a specific Folder, and only the "way" that leads to the relevant Folder (i don't want to include all other Child Folders of every Folder, just the way to the Folder i need)

    Wednesday, August 22, 2012 6:35 AM
  •   

    Can you please post the schema of the tables involved and any relationships between them.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 22, 2012 2:34 PM