在linq中从数据集转换为列表

时间:2012-08-15 07:07:12

标签: linq dataset

我有数据集功能,如下所示,

    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);
        }
    }

我正在尝试将其转换为linq,如下所示,

    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;
        }
    }

但我不知道如何在上面的功能中选择不同的brandDesc(如上一个功能)。我只是使用Distinct()。这样对吗?或者还有其他方法可以实现吗?此外,如果我的旧函数(即上面的第一个)中的查询中存在“case”,那么我将如何在第二个函数中将其转换为linq。转换为linq时还需要担心什么?

1 个答案:

答案 0 :(得分:0)

在每次.Select(i => i.brandDesc)来电之前加上Distinct。您还需要更改List<x>所以xbrandDesc的类型。

如果我要重构整个方法,我会做类似以下的事情。我已经提取了两种形式的查询共同的代码,并在其他几个地方进行了调整。

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();
     }
 }