Answered Returning Var from DAL Layer

  • Wednesday, May 28, 2008 11:58 AM
     
     

     

    Hi

     

    I am working on DAL Layer from which I have to return the Object with Types. If I use var What will be by return type of the method?

     

    Here I have two tables namely Directory and Files with one to many relation. I want to all the directory with files in that directory. Here below is my Query and My Methods return type is List<Directory>

     

    public List<Directory> GetDirectoryWithFiles()

    {

    using (DB oDB = new DB(ConString))

    {

    DataLoadOptions oDataLoadOptions = new DataLoadOptions();

    oDataLoadOptions.LoadWith<Directory>(dir => dir.Files);

    oDB.LoadOptions = oDataLoadOptions;

     

    List<Directory> lstDirectories = (from dir in oDB.GetTable<Directory>()

    where dir.Id == 12

    select new Directory

    {

    DirectoryName = dir.DirectoryName,

    Files = dir.Files.Select(file => new

    {

    fileName = file.Filename,

    ModDate = file.ModDate,

    })

    }).ToList<Directory>();

    return lstDirectories;

    }

    }

     

    Here Files is EntitySet in Directory Class and I am not able store the results to it coz the return type of Select is IEnumerable. If I want to return the Anonymous type from the query then What should be my return type in the Method?

     

    One more Question. I have created Association between these two classes. I thought there is will be single query to get the child records but still it is sending separate SQL Queries to get the child records but in the same connection. Is that Right?

     

    Thanks

    Anandraj.A.

All Replies

  • Wednesday, May 28, 2008 2:58 PM
     
     Answered

    >Here Files is EntitySet in Directory Class and I am not able store the results to it coz the return type of Select is IEnumerable.

     

    Yes, what you really want to do is this:

     

    List<Directory> lstDirectories = (from dir in oDB.GetTable<Directory>()

    where dir.Id == 12

    select dir).ToList();

     

     

    >If I want to return the Anonymous type from the query then What should be my return type in the Method?

     

    The anonymous type is assigned a name at compile time.  This means you can't use the name at design time.

    Badnews: If you must return an anonymous type, You must return object.

    Goodnews: any time you'd need to return an anonymous type, you can make an non-anonymous type with the same properties and return that.

     

     

    > I have created Association between these two classes. I thought there is will be single query to get the child records but still it is sending separate SQL Queries to get the child records but in the same connection. Is that Right?

     

    Trust the sql-profiler.  It will tell you the truth about what is sent over the wire.