返回Web API中的不同行

时间:2016-08-12 20:03:28

标签: c# sql asp.net asp.net-mvc oracle

我们目前正在查询Oracle数据库并以JSON格式返回结果。查询结果返回的重复行数越多。控制器如下所示

public HttpResponseMessage Getdetails([FromUri] string[] Column)
    {
     List<string> selectionStrings = new List<string>();
     string connStr = ConfigurationManager.ConnectionStrings["PConnection"].ConnectionString;
     using (OracleConnection dbconn = new OracleConnection(connStr))
     {
      DataSet userDataset = new DataSet();
      var colDict = new Dictionary<string, string>()
      {
       {"CATEGORY", "STCD_PRIO_CATEGORY_DESCR.DESCR"},
       {"SESSION_NUMBER", "STCD_PRIO_CATEGORY_DESCR.SESSION_NUM"},
       {"SESSION_START_DATE","Trunc(STCD_PRIO_CATEGORY_DESCR.START_DATE)"},
       {"SESSION_START_TIME","STCD_PRIO_CATEGORY_DESCR.START_DATE"}}};
            foreach (string col in Column)
            {
                string selector = colDict[col];
                selectionStrings.Add(string.Format("{0} AS {1}", selector, col));
            }
            string selectString = string.Join(",", selectionStrings);

            var strQuery =   string.Format(@"SELECT {0}
                             FROM 
                             STCD_PRIO_CATEGORY_DESCR", selectString);
     }}}

因此,如果我只是将Select语句中的Distinct添加为

 var strQuery =   string.Format(@"SELECT DISTINCT {0}
                             FROM 
                             STCD_PRIO_CATEGORY_DESCR", selectString);

那应该够了吗?或者我们应该在所有列名称之前放置DISTINCT

1 个答案:

答案 0 :(得分:-1)

使用distinct或者如果仍然遇到问题,可以将数据结果移动到列表中,并使用linq仅选择唯一的行。

NSValue

取自:Select distinct using linq