Results 1 to 2 of 2

Thread: Possible to make this method generic?

  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Possible to make this method generic?

    My C# is not as great as I'd like to believe and Generic definitely not my strong point. (comes from using resharper a few years..)
    Anyhow, I figured out this method (using it for server side processing for jquery datatables) that works fine, but only for one type, meaning I will create a new method for each other type I want to use this method for.

    Was wondering if I could make it "generic". Perhaps my code will explain better (first is the working method, second a bit of pseudo code trying to figure out what I want to do)

    Code:
    private IList<Client> GetClients(int startIndex, int pageSize, IEnumerable<SortedColumn> sortedColumns, out int totalRecordCount, out int searchRecordCount, string searchString)
    {
    	IQueryable<Client> clients = _clientRepository.GetAll();
    	totalRecordCount = clients.Count();
    	if (!string.IsNullOrWhiteSpace(searchString))
    		clients = clients.Where(c => c.Name.ToLower().Contains(searchString.ToLower()));
    
    	searchRecordCount = clients.Count();
    	IOrderedEnumerable<Client> sortedClients = null;
    	foreach (SortedColumn sortedColumn in sortedColumns)
    	{
    		switch (sortedColumn.PropertyName)
    		{
    			case "Name":
    				sortedClients = sortedClients == null
    									? clients.CustomSort(sortedColumn.Direction, cust => cust.Name)
    									: clients.CustomSort(sortedColumn.Direction, cust => cust.Name);
    				break;
    			// ... etc
    		}
    	}
    	return sortedClients != null ? sortedClients.Skip(startIndex).Take(pageSize).ToList() : new List<Client>();
    }
    Shot at making above generic, taking any of my domain entities:

    Code:
    private IList<T> GetData(IList<T> data, int startIndex, int pageSize, IEnumerable<SortedColumn> sortedColumns, out int totalRecordCount, out int searchRecordCount, string searchString)
    {
    	totalRecordCount = data.Count();
    	if (!string.IsNullOrWhiteSpace(searchString))
    		data = data.Where(c => c.ToString().ToLower().Contains(searchString.ToLower()));
    	
    	searchRecordCount = data.Count();
    	IOrderedEnumerable<T> sortedData = null;
    	foreach (SortedColumn sortedColumn in sortedColumns)
    	{
    		var property = "get property from type where property type name equals to sorted column";
    		sortedData = sortedData == null
    									? data.CustomSort(sortedColumn.Direction, cust => cust.Name)
    									: clients.CustomSort(sortedColumn.Direction, cust => cust.Name);    // no hell, i'm lost
    	}
    	return sortedData != null ? sortedData.Skip(startIndex).Take(pageSize).ToList() : new List<Client>();
    }
    Any advice?


  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Possible to make this method generic?

    If you're creating the classes yourself, why not just create an interface and create a list off of the interface?

    It seems the main problem you're running into, is with the sort, so having an interface implement those properties would solve the issue.

    Also, I don't think this line would work:

    C# Code:
    1. data = data.Where(c => c.ToString().ToLower().Contains(searchString.ToLower()));

    Because a .ToString() of the object itself would simply return the object data type, and not the properties within the object itself.

    You could use reflection to get the property values by name as well as do the sort by name, but it seems like overkill if you could just use an interface.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width