none
query sub class RRS feed

  • Question

  • Hello,

    I'm working with EF4.1 and until now I love it. A have a class (Client) with a lot of fields. Retrieving and editing the class with EF works.

    Now I have a grid where I will display (readonly) some columns from the Client class. What is the best method to do it?

    I have some ideas on how to do it but I'm not sure wich one is the best:

    1. Query the client class (this works but I don't want to query all fields do display only 2 of them)

    2. Create a query with anonumoyse type result, but I don't love this aproach because it's easier to work on the designer with a concrete class

    3. Create another class (for example ClientForList) with only the fields I need. This seems like a good aproach but I don't know how to link this new class with the same table. I think in order tu use tha same table for 2 diferentes dbsets they need to inherite ¿no?

    Thanks,

    Alex B.

    Tuesday, November 29, 2011 1:40 PM

Answers

  • Hi,

    You are on the right path.. I would have used either option 2 or 3, as you say option 1 will query to much unneeded data.

    Since you are dependent on a class to do your designer work only option 3 is a good solution for you (some designers also supports anonymous types, but you haven't specified where you want to use this code, so it's hard to tell).

    So, with your option 3, you could simply do something like this:

    //A helper class
    public class ClientGridHelper
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    //The result generation: (LINQ Method)
    var result = context.Clients.Select(row => new ClientGridHelper { Id = row.Id, Name = row.Name }).ToList();
    
    //or (LINQ Query)
    var result = (from row in context.Clients 
                  select new ClientGridHelper { Id = row.Id, Name = row.Name }).ToList();
    

    Hope this helps!


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.
    • Marked as answer by Alex Bibiano Tuesday, November 29, 2011 2:27 PM
    Tuesday, November 29, 2011 2:18 PM

All replies

  • Hi,

    You are on the right path.. I would have used either option 2 or 3, as you say option 1 will query to much unneeded data.

    Since you are dependent on a class to do your designer work only option 3 is a good solution for you (some designers also supports anonymous types, but you haven't specified where you want to use this code, so it's hard to tell).

    So, with your option 3, you could simply do something like this:

    //A helper class
    public class ClientGridHelper
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    //The result generation: (LINQ Method)
    var result = context.Clients.Select(row => new ClientGridHelper { Id = row.Id, Name = row.Name }).ToList();
    
    //or (LINQ Query)
    var result = (from row in context.Clients 
                  select new ClientGridHelper { Id = row.Id, Name = row.Name }).ToList();
    

    Hope this helps!


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.
    • Marked as answer by Alex Bibiano Tuesday, November 29, 2011 2:27 PM
    Tuesday, November 29, 2011 2:18 PM
  • A lot of thanks. This is exactly was I was looking for!!! I have try it and it works.

    Only one question. If I want to use anonymouse type, can I declare a anoynymouse type variable on my form and after doing the query assign it to a BindingSource.DataSource

    Thanks,

    Alex B.

    Tuesday, November 29, 2011 2:27 PM
  • Good to hear it helped you :)

    About the anonymous type and BindingSource question. I am actually not sure, I have never worked with that and anonymous types. But try, you'll probably get an error if it doesn't work :)


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful" if the post helped you to a solution of your problem.
    Tuesday, November 29, 2011 2:36 PM