[英]Getting Distinct Values from linq
I have ad-hoc query which is used generate Gridview.DataSource=DataTable,and I'm trying to get distinct(No duplicates) results from based on the ID(ActionID) but the duplicates still exist.Using the distinct keyword in the select linq statement seems have no effect.(Still struggling with linq).Is there another way to do this? 我有一个临时查询,用于生成Gridview.DataSource = DataTable,并且我试图根据ID(ActionID)从中获得不同的结果(没有重复项),但是重复项仍然存在。在select中使用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();
}
}
Distinct affects all columns, not just the column it precedes. 不同会影响所有列,而不仅是它前面的列。 You could group the rest of the columns ( and add appropriate aggregate functions to the select list).
您可以将其余的列分组(并将适当的聚合函数添加到选择列表中)。 Its going to depend a lot on how your data looks.
它很大程度上取决于您的数据外观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.