views:

463

answers:

4

I use Linq to SQL as data access layer in ASP.NET MVC application. So the query result is a strong typed object. How can I dynamiclly specify which field to show in the page.

For example, the query result has the following fields: FirstName LastName Address Tel

My question is if one user wanna show the Lastname and the Firstname. 2nd user wanna show the Address and Firstname, 3rd etc... Different user has different requirements. So how can I query the database/filter the result based on user's specific requirement(on demand)?


To be more specific, the query result is a set of person information.

public class Person
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
  public string Tel {get;set;}
  public string Tel {get;set;}
}
A: 

Use an if statement based upon user input. I am assuming you stored the User's preference somewhere, in which case the following code would do the trick:

if (showAddress)
{
    var results = from u in Users
                  select new
                  {
                      FirstName = u.FirstName;
                      LastName = u.LastName;
                      Address= u.Address;
                  }
    // Code to display results goes here
}
else
{
    var results = from u in Users
                  select new
                  {
                      FirstName = u.FirstName;
                      LastName = u.LastName;
                  }
    // Code to display results goes here
}
l3a0
Thanks for the solution. BUt it seems a little clumsy and what if there are many fields and many different combinations?
Smallville
+1  A: 

1) User indicates in the UI what results the user wishes to see
2) The Controller interprets this and stores this for later
3) The Controller goes to the DAL and gets the data from the DAL
4) The Controller then modifies the return result somehow according to #2
5) The Controller then passes the modified data to the UI
6) The UI renders the data

I think your disconnect starts at 4 and may extend as far as 6.

The fact is that there are literally thousands of ways to do this. Here's one way to do it in amazingly C#-like pseudocode.

First, I'd create a view model that contains information on what I want to display to the user.

Original Linq to Sql (abbreviated):

public class Person
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
  public string Tel {get;set;}
}

My view model:

public partial class PeopleView
{
  public bool ShowFirstName {get;set;}
  public bool ShowLastName {get;set;}
  public bool ShowTel {get;set;}
  public IEnumerable<Person> People {get;set;}
}

The controller method that preps the model:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PersonDetails(bool showFirstName, 
    bool showLastName, bool showTel)
{
  var viewData = new PeopleView()
    { 
      ShowFirstName = showFirstname, 
      ShowLastName = showLastName, 
      ShowTel = showTel, 
      People = Dal.GetPeople()
    };

  return View(viewData);
}

And here's the View:

<% foreach(var item in ViewData.Model.People){ %>
  <% if(ViewData.Model.ShowFirstName) {%>
    <%= item.FirstName %><br/>
  <% } %>
  <% if(ViewData.Model.ShowLastName) {%>
    <%= item.LasttName %><br/>
  <% } %>
  <% if(ViewData.Model.ShowTel) {%>
    <%= item.Tel %><br/>
  <% } %>
<% } %>
Will
Hi, Will, thank you for the answer. But it is not I'm looking for. My question is if one user wanna show the lastname and firstname. 2nd user wanna show address and first name, 3rd etc... Different user have different requirements. So how can I query the database/filter the result based on user's specific requirement(on demand)?
Smallville
My answer was to get all the data, then merge it with the user's preferences and display appropriately. I guess you were looking for filtering further down the pipeline than that.
Will
A: 

To be more specific, the query result is a set of person information.

public class Person
{
  public string FirstName {get;set;}
  public string LastName {get;set;}
  public string Tel {get;set;}
  public string Addresss{get;set;}
  public string Email{get;set;}
}

now One user want to show only the first name and last name. So it would be Person.FirstName and Person.LastName, another user want to show FirstName and Tel, so the code would be Person.FirstName and Person.Tel

Then how can I program to dynamically show the result based on demand?

Smallville
+1  A: 

Take a look at System.Linq.Dynamic:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

KristoferA - Huagati.com
This is what I'm lookin' for, thanks!
Smallville