简体   繁体   English

如何仅获取具有修改后的特定行值的数据表的第一行

[英]how to get only first row of datatable with modified specific row value

I have query to just summary of total no of jobs running. 我有查询到仅汇总正在运行的作业总数。 now I just want some specific result if there is unique rows found like unique category id with assign job then ok with multiple record set. 现在,我只想要一些特定的结果,如果找到了唯一的行,例如具有分配工作的唯一类别ID,然后可以包含多个记录集。 but no category found if is null then just only pass first record from datatable with modified text with 'ALL' as category name. 但没有找到类别(如果为null),则仅传递数据表中的第一条记录,其中带有“ ALL”作为类别名称的修改文本。 can we achieve this result. 我们能达到这个结果吗?

here is my query and some operations I'm doing with them. 这是我的查询以及我正在对其进行的一些操作。

string str = "";
            DataTable dt = new DataTable();
            str = "SELECT j.[JobID], p.[Id] As PreparedEmailID,p.[Title] AS 'PreparedEmailName',j.[CreatedOn],j.[CompletedOn],j.CategoryID,j.[SubscriberCount],j.[EmailsSent],c.[CategoryName] As SubscriberCategory,(SELECT TOP 1 [Message] FROM [LoggedMessages] WHERE [JobID] =j.[JobID] ORDER BY [LoggedMessageID] DESC) AS 'LoggedMessage',(SELECT [Name] FROM tbl_User_master u WHERE u.Id =j.UserID) As CreatedBy FROM [Jobs] AS j INNER JOIN [tbl_Email_master] AS p ON p.[Id] = j.[PreparedEmailID] INNER JOIN [tbl_User_master] AS u ON u.[Id]=j.[UserID] INNER JOIN tbl_Categories c ON c.Id = j.CategoryID OR (c.Id IS NOT NULL AND j.CategoryID IS NULL) where 1=1 ";
            if (chk_date.Checked == true)
            {
                str += " and ( [CreatedOn] between '" + CommonLogic.Get_Date_From_String(txt_date_from.Text, 1);
                str += "' and '" + CommonLogic.Get_Date_From_String(txt_date_to.Text, 2) + "' )";
            }
            if (string.IsNullOrEmpty(txttitle.Text.Trim()))
            {
                str += string.Empty;
            }
            else
            {
                str += " and p.Title like '%" + txttitle.Text.Trim() + "%'";
            }
            if (ddl_fromuser.SelectedItem.Text.ToString() == ".All")
            {
                str += string.Empty;
            }
            else
            {
                str += " and j.FromuserID = CONVERT(INT," + Convert.ToInt32(ddl_fromuser.SelectedValue.ToString()) + ")";
            }
            if (ddl_subcategories.SelectedItem.Text.ToString() == ".All")
            {
                str += string.Empty;
            }
            else
            {
                str += " and j.CategoryID = CONVERT(INT," + Convert.ToInt32(ddl_subcategories.SelectedValue.ToString()) + ")";
            }
            dt = obj.Get_Data_Table_From_Str(str);
            if (dt.Rows.Count > 1)
            {
                dt.Rows[0]["SubscriberCategory"] = "ALL";
                var topRows = dt.AsEnumerable().FirstOrDefault();
                egrd.DataSource = topRows;
                egrd.DataBind();
            }
            else
            {
                egrd.DataSource = dt;
                egrd.DataBind();
            }
            ViewState["data"] = dt;

how ever this gives me error like no JobID found to this record set. 怎么会给我这样的错误,例如找不到该记录集的JobID。 whether it is still exists in record set. 它是否仍存在于记录集中。

please help me... 请帮我...

well I tried this solution but no success...…….. 好吧,我尝试了这种解决方案,但没有成功…………..

 if (dt.Rows.Count > 1)
            {
                dt.Rows[0]["SubscriberCategory"] = "ALL";
                var topRows = dt.AsEnumerable().GroupBy(j => j.Field<int>("JobID")).Select(j => j.First()).ToList();
                egrd.DataSource = topRows;
                egrd.DataBind();
            }

it's gives me exception like DataBinding: 'System.Data.DataRow' does not contain a property with the name 'JobID'. 它给了我类似DataBinding: 'System.Data.DataRow' does not contain a property with the name 'JobID'.异常DataBinding: 'System.Data.DataRow' does not contain a property with the name 'JobID'.

只需将.ToList()替换为.CopyToDataTable()即可解决我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM