【问题标题】:Converting from dataset to list in linq在linq中从数据集转换为列表
【发布时间】:2012-08-15 15:26:05
【问题描述】:

我有如下数据集函数,

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

【问题讨论】:

  • 对于您关于“案例”的问题,您需要提供一个带有“案例”的查询示例 - 这在单独的问题中会更好,而不是添加到这个问题中。

标签: linq dataset


【解决方案1】:

在每个Distinct 调用之前添加一个.Select(i =&gt; i.brandDesc)。您还需要更改您的List&lt;x&gt;,以便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();
     }
 } 

【讨论】:

  • 如何将以下函数转换为 linq 中的函数。公共字符串 GetUpodStatus() { 使用 (SqlConnection conn = new SqlConnection(this.ConnectionString)) { conn.Open(); SqlCommand command = new SqlCommand("select upod_status from arabia_upod_status", conn); IDataReader r = null; r = this.ExecuteReader(command); if (r.Read()) return r["upod_status"].ToString().ToUpper();否则返回“假”; } }
  • 正如我所说,您应该将此作为单独的问题发布。如果你能展示一些工作 - 就像你在 this 问题中所做的那样 - 你会收到更好的接待。
猜你喜欢
  • 2011-11-12
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 2011-12-25
  • 1970-01-01
  • 2017-06-28
相关资源
最近更新 更多