简体   繁体   中英

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. but no category found if is null then just only pass first record from datatable with modified text with 'ALL' as category name. 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. 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'.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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