来自DataTable的不同值c#(Distinct Values From DataTable c#)

编程入门 行业动态 更新时间:2024-10-26 16:31:48
来自DataTable的不同值c#(Distinct Values From DataTable c#)

我已将xml转换为DataTable 。 我有类似这样的数据表。 它描述了一个特定酒店的房间选择。 如果板类型,房间类型,房价相同,我想把它分成两个房间。

Room 1 Boards Type Room Type Room Price Sleep Up To Room Only Double Standard 301.280 2 Guests Room Only Double Standard 301.280 2 Guests

我想让它像这样。

Room 1 Boards Type Room Type Room Price Sleep Up To Room Only Double Standard 301.280 2 Guests Room 2 Boards Type Room Type Room Price Sleep Up To Room Only Double Standard 301.280 2 Guests

以下是我的代码我试过打破这个使用没有客人。 在那之后,我发现这不是正确的方式来打破这个只使用没有客人。 所以我想打破这个使用3列,并希望打印为2号房间。

PlaceHolder1.Controls.Add(new LiteralControl("<div class='type1 pd10-lr pd15-tb'>")); var noofroom = (from row2 in dt2.AsEnumerable() where (Int64)row2["hotelcode"] == hotelcode select row2.Field<string>("guests")).Distinct(); int a = 1; foreach (var rcount in noofroom) { PlaceHolder1.Controls.Add(new LiteralControl("<div class='rhead'>" + "Room " + a + "" + "</div>" + "<table class='roomtbl'>" + "<tr>" + "<td class='tdaltbg'>" + "<strong>&nbsp;" + "Board Type</strong></td>" + "<td class='style30'>" + "<strong>&nbsp;" + "Room Type</strong></td>" + "<td class='tdaltbg'>" + "<strong>&nbsp;&nbsp; Room Price</strong></td>" + "<td class='style30'>" + "<strong>&nbsp;" + "Sleep Up To</strong></td>" + "<td class='tdaltbg'>" + "<strong></strong></td>" + "</tr>")); DataRow[] drs = dt2.Select("hotelcode='" + hotelcode + "' AND guests='" + rcount + "'"); foreach (DataRow dr in drs) { PlaceHolder1.Controls.Add(new LiteralControl("<tr>" + "<td class='tdaltbg'>" + "&nbsp;" + "" + dr["board"] + "</td>" + "<td class='style30'>" + "" + dr["roomtype"] + "</td>" + "<td class='tdaltbg'>" + "&nbsp;" + "£ " + dr["amount"] + "</span></td>" + "<td class='style30'>" + "&nbsp;" + " " + dr["guests"] + " Guests</span></td>" + "<td class='tdaltbg'>")); PlaceHolder1.Controls.Add(new LiteralControl("<input type='radio' name='t1' data-price='308.560' checked='checked'/>" + "</td>" + "</tr>")); } PlaceHolder1.Controls.Add(new LiteralControl("</table>")); a++; }

I have converted xml to DataTable . I Have a datatable similar like this. It's describe the room options in one specific hotel. I want to break this into two room if boards type, room type, room price are same.

Room 1 Boards Type Room Type Room Price Sleep Up To Room Only Double Standard 301.280 2 Guests Room Only Double Standard 301.280 2 Guests

I want to make it similar like this.

Room 1 Boards Type Room Type Room Price Sleep Up To Room Only Double Standard 301.280 2 Guests Room 2 Boards Type Room Type Room Price Sleep Up To Room Only Double Standard 301.280 2 Guests

Below Is My Code Which I have tried break this using no of guests. after did that I found It's not right way to break this only using no of guests. so I want to break this using 3 columns and want to print as room 2.

PlaceHolder1.Controls.Add(new LiteralControl("<div class='type1 pd10-lr pd15-tb'>")); var noofroom = (from row2 in dt2.AsEnumerable() where (Int64)row2["hotelcode"] == hotelcode select row2.Field<string>("guests")).Distinct(); int a = 1; foreach (var rcount in noofroom) { PlaceHolder1.Controls.Add(new LiteralControl("<div class='rhead'>" + "Room " + a + "" + "</div>" + "<table class='roomtbl'>" + "<tr>" + "<td class='tdaltbg'>" + "<strong>&nbsp;" + "Board Type</strong></td>" + "<td class='style30'>" + "<strong>&nbsp;" + "Room Type</strong></td>" + "<td class='tdaltbg'>" + "<strong>&nbsp;&nbsp; Room Price</strong></td>" + "<td class='style30'>" + "<strong>&nbsp;" + "Sleep Up To</strong></td>" + "<td class='tdaltbg'>" + "<strong></strong></td>" + "</tr>")); DataRow[] drs = dt2.Select("hotelcode='" + hotelcode + "' AND guests='" + rcount + "'"); foreach (DataRow dr in drs) { PlaceHolder1.Controls.Add(new LiteralControl("<tr>" + "<td class='tdaltbg'>" + "&nbsp;" + "" + dr["board"] + "</td>" + "<td class='style30'>" + "" + dr["roomtype"] + "</td>" + "<td class='tdaltbg'>" + "&nbsp;" + "£ " + dr["amount"] + "</span></td>" + "<td class='style30'>" + "&nbsp;" + " " + dr["guests"] + " Guests</span></td>" + "<td class='tdaltbg'>")); PlaceHolder1.Controls.Add(new LiteralControl("<input type='radio' name='t1' data-price='308.560' checked='checked'/>" + "</td>" + "</tr>")); } PlaceHolder1.Controls.Add(new LiteralControl("</table>")); a++; }

最满意答案

请试试这个。 这里可以将多个列作为参数传递,并将其作为distinct传递。

Please try this. Here multiple columns can be passed as parameter and true for distinct.

更多推荐

本文发布于:2023-08-06 07:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1445029.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DataTable   Distinct   Values

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!