0

I want to get two table combine data using linq c# lambda expression and get fix number of column get in the list. I have try this code but I don't know how to write query in linq lambda expression.

this is my method:

public ActionResult GetUserList(string searchRequest)
{          
    List<User> userList = new List<User>();              
    if (searchRequest != null)
    {
        if (searchRequest == "All")
        {
// here i want to write select query but how i don't know
            userList = db.user.ToList();
        }
        else if (searchRequest == "Flight") 
        {
            userList = db.user
                .Where(t => t.type_id == (int)ServiceTypeEnum.Flight)
                .ToList();
        }                   
    }                                       
    return Json(new { data = userList });
}

any one have the idea about this query then please let me know how can do.

7
  • It would be helpfull if you could be more specific. Example data would be good and how your result should look like. Commented Mar 10, 2017 at 12:09
  • it is already getting translated in to select query, do you want to get specific columns instead? Commented Mar 10, 2017 at 12:09
  • @MightyBadaboom look like something want in user table get only fname,lname,etc.. Commented Mar 10, 2017 at 12:11
  • @EhsanSajjad yes i want get specific column instead Commented Mar 10, 2017 at 12:14
  • db.user.Select(x=> new {x.Col1,x.Col2}).ToList(); Commented Mar 10, 2017 at 12:49

3 Answers 3

1

The issue that you will experience with lambdas where you select specific fields is that the result is normally an anonymous type. Anonymous types from two different queries cannot easily be joined together in a list because the compiler cannot verify the structure or equality of the types.

There are other ways around this...

The best practice approach is to create a formal type definition and use that so that you can manipulate your objects outside of your lambda expressions. Note here that I have assumed a simple example structure that is a sub-set of user:

public ActionResult GetUserList(string searchRequest)
{
    try
    {
        List<UserSearchResult> UserList = new List<UserSearchResult>();
        if (searchRequest != null)
        {
            if (searchRequest == "All")
            {
                UserList.AddRange(db.user.Select(u => new UserSearchResult { Title = u.Title, FirstName = u.Firstname, LastName = u.Lastname })); // here i want to write select query but how i don't know
            }
            else if (searchRequest == "Flight")
            {
                UserList.AddRange(db.user.Where(t => t.type_id == (int)ServiceTypeEnum.Flight)
                    .Select(u => new UserSearchResult { Title = u.Title, FirstName = u.Firstname, LastName = u.Lastname }));
            }
        }
        return Json(new { data = UserList });
    }
    catch (Exception ex)
    {
        throw;
    }
    return Json(null);
}

public class UserSearchResult
{
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Because we have explicitly cast the result of our selection of specific fields to a formal type, we can now use that type in operations outside of your queries and can even manipulate the values.

Sign up to request clarification or add additional context in comments.

Comments

1

Define UserDto class for columns that you want select

public class UserDto
{
    public int Id{get;set;}

    public int Name{get;set;}

    //Other Properties
}

Then change your code to following

public ActionResult GetUserList(string searchRequest)
{
    try
    {                            
        if (searchRequest != null)
        {
            IQueryable<User> query; 

            if (searchRequest == "All")
            {
                query = db.user.AsQueryable(); // here i want to write select query but how i don't know
            }
            else if (searchRequest == "Flight") 
            {
                UserList = db.user.Where(t => t.type_id == (int)ServiceTypeEnum.Flight);
            }  

            if(query != null)
            {
                var list = query.Select(e=> new UserDto
                {
                    Id = e.Id,
                    Name = e.Name
                    //Other properties
                }).ToList();

                return Json(new { data = list });
            }   
        }                                       

   }
   catch (Exception ex)
   {
       throw;
   }
   return Json(null);
}

Comments

0

I think the local variable is holding you back. Just return the result you want.

public ActionResult GetUserList(string searchRequest)
{
    if (searchRequest == "All")
    {
       var users = db.user
           .Select(user => new {user.Name, user.Address.ZipCode})
           .ToList();

       return ToJson(users);
    }
    else if (searchRequest == "Flight") 
    {
        List<User> users = db.user
            .Where(t => t.type_id == (int)ServiceTypeEnum.Flight)
            .ToList();

        return ToJson(users);
    }

    return ToJson(new List<User>());
}

private ActionResult ToJson<T>(T list)
{
    return Json(new { data = list });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.