0

我有用于生成 Gridview.DataSource=DataTable 的临时查询,并且我正在尝试根据 ID(ActionID)获取不同的(无重复)结果,但重复项仍然存在。在选择中使用 distinct 关键字linq 语句似乎没有效果。(仍在与 linq 苦苦挣扎)。还有另一种方法可以做到这一点吗?

    DataTable dtSerachResults = new DataTable(); 
string qryStr = @"SELECT Distinct [ActionID]
                                    , [Title],convert(varchar(11),[DateRaised],109)as  DateRaised
                                , convert(varchar(11)
                                ,[TargetCompletionDate] ,109) as TargetCompletionDate 

                                ,dbo.fn_GetPersonelName(fk_PersonnelID) as Responsible
                                ,dbo.fn_GetStatusNameByID(fk_StatusID)   as [Status]
                                ,dbo.fn_GetClientNameByID(fk_CustomerID)as Client
                                ,dbo.fn_GetLocationByLocationID(fk_LocationID) as Location
                                ,dbo.fn_GetClientNameByID(fk_CustomerID) as Client
                                ,dbo.fn_GetSourceNameByID(fk_SourceID)as [Source]
                                , dbo.fn_GetPersonelName(fk_RaisedBy) as RaisedBy
                                ,dbo.fn_GetPersonelName(dbo.fn_GetActionItemPrimaryResponsibleEmployeeID([ActionID]))as [Primary]
                               ,DATEDIFF(DD,DateRaised,GETDATE()) as Diff
                                ,IsApproved=
                                case IsApproved 
                                    when 1 then 'Approved'
                                    when 0 then 'Rejected'
                                    ELSE  'Waiting'
                                End 
                        FROM [ActionTrackerTable] att
                        inner join [dbo].[ActionResponsibilitiesTable] art on art.fk_ActionID=att.[ActionID] ";

                //title
                if (!string.IsNullOrEmpty(ActionTracker.Title))
                {
                    qryStr += " and Title like '" + ActionTracker.Title + "%'";
                }

                //dateraised
                if (ActionTracker.DateRaised > DateTime.MinValue)
                {
                    qryStr += " and DateRaised >= '" + ActionTracker.DateRaised.ToString() + "'";
                }
                //targetgedate
                if (ActionTracker.TargetCompletionDate > DateTime.MinValue)
                {
                    qryStr += " and TargetCompletionDate>= '" + ActionTracker.TargetCompletionDate.ToString() + "'";
                }

                //Location
                if (ActionTracker.Location != null)
                {
                    qryStr += " and   fk_LocationID= " + ActionTracker.Location.LocationID.ToString() + "";
                }
                //client
                if (ActionTracker.Customer != null)
                {
                    qryStr += " and   fk_CustomerID= " + ActionTracker.Customer.CustomerID.ToString() + "";
                }
                //source
                if (ActionTracker.Source != null)
                {
                    qryStr += " and   fk_SourceID= " + ActionTracker.Source.SourceID.ToString() + "";
                }
                //approval status
                if (ActionTracker.SearchApprovalStr == "Approved")
                {
                    qryStr += " and   IsApproved= " + 1.ToString();
                }
                if (ActionTracker.SearchApprovalStr == "Rejected")
                {
                    qryStr += " and   IsApproved= " + 0.ToString();
                }

                if (ActionTracker.SearchApprovalStr == "Waiting")
                {
                    qryStr += " and   IsApproved is null";
                }
                //status
                if (ActionTracker.Status !=null)
                {
                    if (ActionTracker.Status.StatusID>0)
                    {
                        qryStr += " and   [fk_StatusID]= "+ActionTracker.Status.StatusID.ToString();
                    }
                }

                //ref num
                if (ActionTracker.ActionTrackerID > 0)
                {
                    qryStr += " and   ActionID = " + ActionTracker.ActionTrackerID.ToString();
                }

                if ( (strSearchTerm)=="All" ||(strSearchTerm)=="Select Employee"  )
                {                                )";

                    //rrun qry
                     //add the order str
                     qryStr += " order by ActionID desc ";
                     System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
                     using (sqlConn = ConnectionClass.CreateConnection.publicGetConn())
                     {
                         sqlConn.ConnectionString = ConnectionClass.CreateConnection.getConnectionString();
                         sqlConn.Open();
                         if (sqlConn.State == System.Data.ConnectionState.Open)
                         {
                             using (System.Data.SqlClient.SqlCommand sqlCMD = new System.Data.SqlClient.SqlCommand())
                             {
                                 sqlCMD.CommandText = qryStr;
                                 sqlCMD.CommandType = System.Data.CommandType.Text;
                                 sqlCMD.Connection = sqlConn;
                                 //parameters
                                 //no 1 is reponsbile
                                 //sqlCMD.Parameters.AddWithValue("@responsibleEmployeeID", responsibleEmployeeID);

                                 //return value

                                 System.Data.SqlClient.SqlDataReader rdr = sqlCMD.ExecuteReader();
                                 //get dataset
                                 DataSet DS = new DataSet();
                                 //load dataset in table
                                 DataTable dt = new DataTable();
                                 dt.Load(rdr);

                                 DS.Tables.Add(dt);
                //get Distinct rows based on the Thier rows 


 var q2 = (from dr in dt.AsEnumerable() select dr).Distinct().CopyToDataTable();
                                     dtSerachResults = (from dr in dt.AsEnumerable() select dr).Distinct().CopyToDataTable();


                }


            }
4

1 回答 1

0

Distinct 影响所有列,而不仅仅是它前面的列。您可以对其余列进行分组(并将适当的聚合函数添加到选择列表中)。它在很大程度上取决于您的数据的外观。

于 2013-09-20T15:38:36.780 回答