0

我有如下数据集功能,

    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 时还有其他需要担心的事情吗?

4

1 回答 1

0

在每次通话.Select(i => i.brandDesc)之前放一个。Distinct你还需要改变你的List<x>所以x是什么类型brandDesc

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

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();
     }
 } 
于 2012-08-15T07:11:40.263 回答