none
Return Specific Fields RRS feed

  • Question

  • Hi,

    How do I return specific fields from below query? I just want to use t.Username, t.Address

    Thanks

                ICollection<UserView> userCollection = null;
                using (myDataEntities myEntities = new myDataEntities())
                {
                    IQueryable<UserView> processQuery = from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select t;
                    // populate collection
                    userCollection = new List<UserView>(processQuery);
                }

    Tuesday, April 9, 2013 8:29 AM

Answers

  • Hi again,

    Yo can perform it as follows:

                    IQueryable<UserView> processQuery = (from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new UserView{ 
                                                                               Username = t.Username, 
                                                                               Address = t.Address
                    }).AsQueryable();

    Or in this way:

                    List<UserView> processQuery = (from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new UserView{ 
                                                                               Username = t.Username, 
                                                                               Address = t.Address
                    }).ToList();

    Depending of your needs...

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Tuesday, April 9, 2013 11:03 AM
  • Hi obrienkev;

    To return an Anonymous type change this query 

    IQueryable<UserView> processQuery = from t in myEntities.UserStatus
                                        where t.UserID.Equals(userID)
                                        && t.UserDate == period
                                        select t;
                                        
    //  To this. Please note the var return type.                                    
    var processQuery = from t in myEntities.UserStatus
                       where t.UserID.Equals(userID)
                             && t.UserDate == period
                       select new {
                           t.Username, 
                           t.Address
                       };

    Now If UserView is NOT part the Entity Framework data model but a independent class having the two properties Username and Address then you can do this.

    IQueryable<UserView> processQuery = from t in myEntities.UserStatus
                                        where t.UserID.Equals(userID)
                                              && t.UserDate == period
                                        select new UserView() {
                                            Username = t.Username, 
                                            Address = t.Address
                                        };


    Fernando (MCSD)

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

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, April 9, 2013 2:06 PM

All replies

  • Hi,

    You can return an anonymous class in this way:

                    IQueryable<UserView> processQuery = from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new { t.Username, t.Address};

    Or, also a typed class more or less in this way:

                    IQueryable<UserView> processQuery = from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new UserView{ 
                    Username = t.Username, 
                    Address = t.Address
               };

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.


    • Edited by JA Reyes Tuesday, April 9, 2013 8:33 AM Syntax
    Tuesday, April 9, 2013 8:32 AM
  • Hi,

    Using the second option gives this error... "The entity or complex type 'MyDataModel.UserView' cannot be constructed in a LINQ to Entities query."

    The first option doesn't return anonymous type?

    • Edited by obrienkev Tuesday, April 9, 2013 8:46 AM
    Tuesday, April 9, 2013 8:46 AM
  • Hi,

    Try to use ToList() method:

                    IQueryable<UserView> processQuery = (from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new UserView{ 
                                                                               Username = t.Username, 
                                                                               Address = t.Address
                    }).ToList();

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Tuesday, April 9, 2013 10:33 AM
  • Hi,

    Try to use ToList() method:

                    IQueryable<UserView> processQuery = (from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new UserView{ 
                                                                               Username = t.Username, 
                                                                               Address = t.Address
                    }).ToList();

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Getting this error...

    Cannot implicitly convert type 'System.Collections.Generic.List<UserView>' to 'System.Linq.IQueryable<UserView>'. An explicit conversion exists (are you missing a cast?)

    Tuesday, April 9, 2013 10:59 AM
  • Hi again,

    Yo can perform it as follows:

                    IQueryable<UserView> processQuery = (from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new UserView{ 
                                                                               Username = t.Username, 
                                                                               Address = t.Address
                    }).AsQueryable();

    Or in this way:

                    List<UserView> processQuery = (from t in myEntities.UserStatus
                                                                          where t.UserID.Equals(userID)
                                                                          && t.UserDate == period
                                                                          select new UserView{ 
                                                                               Username = t.Username, 
                                                                               Address = t.Address
                    }).ToList();

    Depending of your needs...

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Tuesday, April 9, 2013 11:03 AM
  • Hi obrienkev;

    To return an Anonymous type change this query 

    IQueryable<UserView> processQuery = from t in myEntities.UserStatus
                                        where t.UserID.Equals(userID)
                                        && t.UserDate == period
                                        select t;
                                        
    //  To this. Please note the var return type.                                    
    var processQuery = from t in myEntities.UserStatus
                       where t.UserID.Equals(userID)
                             && t.UserDate == period
                       select new {
                           t.Username, 
                           t.Address
                       };

    Now If UserView is NOT part the Entity Framework data model but a independent class having the two properties Username and Address then you can do this.

    IQueryable<UserView> processQuery = from t in myEntities.UserStatus
                                        where t.UserID.Equals(userID)
                                              && t.UserDate == period
                                        select new UserView() {
                                            Username = t.Username, 
                                            Address = t.Address
                                        };


    Fernando (MCSD)

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

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, April 9, 2013 2:06 PM