Forum General

Linq Query

SanjayTejaniSanjayTejani USMember ✭✭✭

Hi All,

I am getting student detail from database using linq query.

My requirement is I don't required only one column like FirstName , so how can i do that using linq query.

Please do not refer iteration loop

Tagged:

Best Answers

  • AlexDunnAlexDunn US ✭✭✭
    Accepted Answer

    You can use Select for this:

    ...
    .Select(student => student.FirstName);
    

    Or cast it into a new type

    ...
    .Select(student => new { FirstName = student.FirstName });
    

    If you aren't using extensions:

    var studentNames = from student in students
                                            select student.FirstName;
    

    Make sure your Select call is at the end though because it DOES return an IEnumerable

  • AlexDunnAlexDunn US ✭✭✭
    Accepted Answer

    Yeah. It's exactly how you would write it in SQL as well. There is a nice way to do this by creating a Data Transfer Object (DTO) as a class that represents something similar to your Data Model with only the properties you want to pass around. This DTO can take a constructor parameter of your Data Model and map it to your DTOs properties. THEN, you can use Select to select out your DTO from your database.

  • AlexDunnAlexDunn US ✭✭✭
    edited December 2016 Accepted Answer

    Here's an example:

    let's say you have a User data model (I'm going to shorten some things to make it easier to type):

    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }
    

    Then you create a User DTO (note it doesn't have a password property):

    public class UserDTO
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public UserDTO(User dataModel)
        {
            Id = dataModel.Id;
            Username = dataModel.Username;
        }
    }
    

    Then in your query, you would call:

    ///...
    .Select(user => new UserDTO(user));
    

    This would give you your IEnumerable<UserDTO>.

Answers

  • AlexDunnAlexDunn USMember ✭✭✭
    Accepted Answer

    You can use Select for this:

    ...
    .Select(student => student.FirstName);
    

    Or cast it into a new type

    ...
    .Select(student => new { FirstName = student.FirstName });
    

    If you aren't using extensions:

    var studentNames = from student in students
                                            select student.FirstName;
    

    Make sure your Select call is at the end though because it DOES return an IEnumerable

  • SanjayTejaniSanjayTejani USMember ✭✭✭

    Hi @AlexDunn

    I do not required FirstName in my result. above code is not working for me.

  • AlexDunnAlexDunn USMember ✭✭✭
    Accepted Answer

    Yeah. It's exactly how you would write it in SQL as well. There is a nice way to do this by creating a Data Transfer Object (DTO) as a class that represents something similar to your Data Model with only the properties you want to pass around. This DTO can take a constructor parameter of your Data Model and map it to your DTOs properties. THEN, you can use Select to select out your DTO from your database.

  • AlexDunnAlexDunn USMember ✭✭✭
    edited December 2016 Accepted Answer

    Here's an example:

    let's say you have a User data model (I'm going to shorten some things to make it easier to type):

    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }
    

    Then you create a User DTO (note it doesn't have a password property):

    public class UserDTO
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public UserDTO(User dataModel)
        {
            Id = dataModel.Id;
            Username = dataModel.Username;
        }
    }
    

    Then in your query, you would call:

    ///...
    .Select(user => new UserDTO(user));
    

    This would give you your IEnumerable<UserDTO>.

  • SanjayTejaniSanjayTejani USMember ✭✭✭

    Thanks @AlexDunn

Sign In or Register to comment.