Q:

Converting from dataset to list in linq

I have dataset function which is as below,
    public DataSet GetUpodBrandList(string criteria, string locationId)
    {
        using (SqlConnection conn = new SqlConnection(this.ConnectionString))
        {
            string query;
            if (criteria == "")
                query = "select distinct brandDesc " +
                        "from arabia_upod_item_master " +
                        "where locationId = '" + locationId + 
                        "' order by brandDesc";
            else
                query = "select distinct brandDesc " +
                        "from arabia_upod_item_master " +
                        "where brandDesc like '%" + criteria + "%' " +
                        "and locationId = '" + locationId + "' 
                        order by brandDesc";
            conn.Open();
            SqlCommand command = new SqlCommand(query, conn);
            return this.ExecuteDatasetStoredProc(command);
        }
    }

I am trying to convert it into linq as follow,      
    public static List<DataContext.arabia_upod_item_master> GetUpodBrandList(
                                                               string criteria,
                                                               string locationId)
    {
        List<DataContext.arabia_upod_item_master> m = 
            new List<DataContext.arabia_upod_item_master>();
        using (var db = UpodDatabaseHelper.GetUpodDataContext())
        {
            db.ObjectTrackingEnabled = false;
            if (criteria == "")
                m = db.arabia_upod_item_masters.Where(
                          i => i.locationId == Convert.ToInt32(locationId))
                    .OrderBy(i => i.brandDesc)
                    .Distinct()
                    .ToList();
            else
                m = db.arabia_upod_item_masters.Where(
                        i => i.brandDesc.Contains(criteria) && 
                        i.locationId == Convert.ToInt32(locationId))
                    .OrderBy(i => i.brandDesc)
                    .Distinct()
                    .ToList();
            return m;
        }
    }

But I don't know how to select distinct brandDesc in the above function (as in the previous function). I am simply using Distinct(). Is it right? or is there any other way to achieve it? Also, if there was 'case' in query in my old function (i.e the first one above) then how will i convert it to linq in the second function. Any other things to worry about while converting to linq?

A:

Put a .Select(i => i.brandDesc) just before each Distinct call. You'll also need to change your List<x> so x is whatever the type of brandDesc is.
If I were to refactor you whole method, I'd do something like the following. I've pulled out code that is common to both forms of the query, and tweaked in a couple of other places.
public static IList<string/* I assume*/>GetUpodBrandList(
    string criteria, string locationId)
 {
     // only do this once, not once per item.
     int locatId = Convert.ToInt32(locationId);

     using (var db = UpodDatabaseHelper.GetUpodDataContext())
     {
         db.ObjectTrackingEnabled = false;

         // This is a common start to both versions of the query.
         var query = db.arabia_upod_item_masters
             .Where(i => i.locationId == locatId);

         // This only applies to one version of the query.
         if (criteria != "")
         {
             query = query.Where(i => i.brandDesc.Contains(criteria));
         }

         // This is a common end to both version of the query.           
         return query
             .Select(i => i.brandDesc)
             .Distinct()
             // Do the ordering after the distinct as it will
             // presumably take less effort?
             .OrderBy(i => i.brandDesc)
             .ToList();
     }
 } 

