none
Help with return type in my LINQ Repository RRS feed

  • Question

  • Hello

     

    In my early beginnings of LINQ, I've ran into a small problem.

    I've created a simple repository for my data, inside this repository simple businesslogic will occur once in a while. And for now I just wanted a count of user roles that a user has.

     

    Please take a look here

    Code Snippet

    public IQueryable<User> GetAll()
            {  
                CJ2SystemDataContext dc = connection.GetContext();

                try
                {
                    var users = from u in dc.Users
                                select new{
                                    u.active_directory_user_name,
                                    u.email_address,
                                    role_count = u.UserRoles.Count,
                                    u.is_active
                                };

                        return users;
                }
                catch (Exception)
                {
                   
                    throw;
                }
            }

     

     

     

    Naturally this will fail, no wonder, but what return type should I use when trying to keep things seperated? I've tried to manually add a new property to the User class, but...

     

    Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) 

     

    afterword..

    Well... I now there's a LinqDataSource and that this has an event on Selecting and then I should be able to create custom properties in a sort of abstraction layer, but this is overkill imho to create a datasource for each business rule/ method Smile

    Have I misunderstood something, regarding that?

     

    Thursday, May 22, 2008 5:14 PM

Answers

  • Hi Janus007,

     

    in the

    Code Snippet

    select new{
                                    u.active_directory_user_name,
                                    u.email_address,
                                    role_count = u.UserRoles.Count,
                                    u.is_active
                                };

     

     

    Part of your code block you are actually creating a new Anonymous type that is not equal to "User" type that you are dealing with. However if you amend it a bit as following it should work :

     

    Code Snippet

    public IQueryable<User> GetAll()
            {  
                CJ2SystemDataContext dc = connection.GetContext();

                try
                {
                    var users = from u in dc.Users
                                select new User(){
                                    active_directory_user_name =  u.active_directory_user_name,
                                    email_address = u.email_address,
                                    role_count = u.UserRoles.Count,
                                    is_active = u.is_active
                                };

                        return users;
                }
                catch (Exception)
                {
                   
                    throw;
                }
            }

     

     

    As you see I made the type explicit to match with the type we are sending. (I assume you already defined role_count property on your  User class).

     

    There is nothing wrong with the usage of IQueryable, it is innocent Smile

     

    Hope this is what you needed.

    Thursday, May 22, 2008 6:05 PM

All replies

  • Hi Janus007,

     

    in the

    Code Snippet

    select new{
                                    u.active_directory_user_name,
                                    u.email_address,
                                    role_count = u.UserRoles.Count,
                                    u.is_active
                                };

     

     

    Part of your code block you are actually creating a new Anonymous type that is not equal to "User" type that you are dealing with. However if you amend it a bit as following it should work :

     

    Code Snippet

    public IQueryable<User> GetAll()
            {  
                CJ2SystemDataContext dc = connection.GetContext();

                try
                {
                    var users = from u in dc.Users
                                select new User(){
                                    active_directory_user_name =  u.active_directory_user_name,
                                    email_address = u.email_address,
                                    role_count = u.UserRoles.Count,
                                    is_active = u.is_active
                                };

                        return users;
                }
                catch (Exception)
                {
                   
                    throw;
                }
            }

     

     

    As you see I made the type explicit to match with the type we are sending. (I assume you already defined role_count property on your  User class).

     

    There is nothing wrong with the usage of IQueryable, it is innocent Smile

     

    Hope this is what you needed.

    Thursday, May 22, 2008 6:05 PM
  •  

    Ahh thank you, exactly what I needed.

     

    Could you please tell me the purpose of LinqDataSource also? Is it just another fancy blackbox?

     

    Thursday, May 22, 2008 6:48 PM
  • You are welcome. About LinqDataSource , to cut a long story short, you are right - I liked the term fancy blackbox Smile

     

    It is an another DataSource pattern implementor that has abilities to CRUD IQueryable s and show them in the web pages via controls like ListView & GridView. If you are working in connected mode, it can speed up your data interaction process with bindable web controls.

     

    Here is scottgu's tutorial on Linq datasource : http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx .

     

     

    Thursday, May 22, 2008 8:06 PM