列值不能重复

时间:2018-04-30 07:00:23

标签: c# asp.net linq

我正在尝试生成Excel文件并附加到邮件中。在那个excel文件中我插入数据。我收到了BDname和Email。这两个可能重复。我希望显示这些重复的列值是单一的。

结果如下:

BDName|BDEmail|Process Id|Customer Name|Email
---------------------------------------------
siva  |a@a.com|a1        |hari          |h@h.com
---------------------------------------------
siva  |a@a.com|a2        |hari          |h@h.com
---------------------------------------------
siva  |a@a.com|a3        |hari          |h@h.com
---------------------------------------------
siva  |a@a.com|a3        |hari          |h@h.com
---------------------------------------------
siva  |a@a.com|a3        |hari          |h@h.com
---------------------------------------------
kumar |k@k.com|a4        |krishna       |t@t.com
---------------------------------------------
kumar |k@k.com|a4        |krishna       |t@t.com
---------------------------------------------
kumar |k@k.com|a5        |krishna       |t@t.com
---------------------------------------------
kumar |k@k.com|a6        |krishna       |t@t.com
---------------------------------------------
kumar |k@k.com|a7        |krishna       |t@t.com
---------------------------------------------
kumar |k@k.com|a7        |krishna       |t@t.com
---------------------------------------------
krishna|p@p.com|a3       |krishna       |t@t.com
---------------------------------------------
raju  |r@r.com|a3        |babu          |b@b.com
---------------------------------------------
raju  |r@r.com|a3        |babu          |b@b.com
---------------------------------------------------

期待结果应该如下:

BDName|BDEmail|Process Id|Customer Name|Email
---------------------------------------------
      |a@a.com|a1        |hari          |h@h.com
       ------------------------------------------
      |       |a2        |hari          |h@h.com
       ------------------------------------------
siva  |       |a3        |hari          |h@h.com
       ------------------------------------------
      |       |a3        |hari          |h@h.com
       ------------------------------------------
      |       |a3        |hari          |h@h.com
-----------------------------------------------
      |k@k.com|a4        |krishna       |t@t.com
       ------------------------------------------
      |       |a4        |krishna       |t@t.com
       ------------------------------------------
kumar  |       |a5        |krishna       |t@t.com
       ------------------------------------------
      |       |a6        |krishna       |t@t.com
-------------------------------------------------
krishna|p@p.com|a3       |krishna       |t@t.com
  ------------------------------------------------
raju  |r@r.com|a3        |babu          |b@b.com
      ------------------------------------------
      |       |a3        |babu          |b@b.com
--------------------------------------------------

我的代码是:

StringBuilder str = new StringBuilder();       
str.Append("<table border=" + "1px" + "b>");  
str.Append("<thead>");      
str.Append("<tr>");  
str.Append("<td colspan='2' align='center'><b><font face=Arial Narrow size=2>BD Information </ font></b></td>");  
str.Append("<td colspan='7' align='center'><b><font face=Arial Narrow size=2>Customer Information</ font></b></td>");  
str.Append("</tr>");    
str.Append("<tr>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>BD Name</ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>BD Email</ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2> Process Id </ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>Customer Name</ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>Email</ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>Mobile No.</ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>Case Number</ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>Product Name</ font></b></td>");  
str.Append("<td  align='center'><b><font face=Arial Narrow size=2>Quantity</ font></b></td>");  
str.Append("</thead>");  
str.Append("</tr>");      
var userids = db.Users.Where(x => x.RoleId == 1 || x.RoleId == 2|| x.RoleId == 3 || x.RoleId == 4|| x.RoleId == 5 || x.RoleId == 6).Select(x=>x).ToList();  
foreach (var item1 in userids)  
{  
var BDName = item1.FullName;  
var BDEmail = item1.Email;  
var list = db.Purchases.Where(x => x.SubmittedBy == item1.Id).Select(x => x).ToList();                                             
foreach (var item2 in list)  
{                                     
var CustomerName = db.Users.Where(x => x.Id == item2.UserId).Select(x => x.FullName).FirstOrDefault();  
var CustomerEmail = db.Users.Where(x => x.Id == item2.UserId).Select(x => x.Email).FirstOrDefault();  
var Mobile = db.Users.Where(x => x.Id == item2.UserId).Select(x => x.Mobile).FirstOrDefault();  
var ProcessName = db.Services.Where(x => x.Id == item2.ServiceId).Select(x => x.ProductName).FirstOrDefault();  
var Quantity = item2.Quantity;  
var ProcessId = item2.ProcessId;  
var CaseNo = db.ServiceRequests.Where(x => x.UserId == item2.UserId && x.ServiceId == item2.ServiceId).Select(x => x.SugarCRMCaseNumber).FirstOrDefault();                           
str.Append("<tr>");    
str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + BDName+ " </font></td>");  
str.Append("<td><a href="+""+"><font face=Arial Narrow size=" + "14px" + ">" + BDEmail + "</font></a></td>");  
str.Append("<td><font face=Arial Narrow size=" + "14px" + "> "+ ProcessId + " </font></td>");  
str.Append("<td><font face=Arial Narrow size=" + "14px" + "> " +CustomerName + " </font></td>");  
str.Append("<td><a href=" + "" + "><font face=Arial Narrow size=" + "14px" + ">" + CustomerEmail + " </font></a></td>");  
str.Append("<td><font face=Arial Narrow size=" + "14px" + "> " + Mobile + "</font></td>");  
str.Append("<td><font face=Arial Narrow size=" + "14px" + "> "+ CaseNo + " </font></td>");  
str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + ProcessName + "</font></td>");  
str.Append("<td><font face=Arial Narrow size=" + "14px" + "> " + Quantity + " </font></td>");                                     
str.Append("</tr>");  
}  
}  
str.Append("</table>");  

我想要这样的结果

enter image description here

这是我得到的结果:

result image

当我应用rowspan时,结果如下:

enter image description here

2 个答案:

答案 0 :(得分:1)

由于您循环访问用户ID(列表),下一个即将推出的项目将始终与上述用户相同。

您可以添加rowspan以扩展到多行

foreach (var item1 in userids)  
{ 
   var BDName = item1.FullName;  
   var BDEmail = item1.Email;  
   var list = db.Purchases.Where(x => x.SubmittedBy == item1.Id).Select(x => x).ToList();
   // check
   if(list.Count > 0){
   //use rowspan to extend to multiple rows.   
     str.Append("<td rowspan="+list.Count+"><font face=Arial Narrow size=14px>" + BDName+ " </font></td>");  
     str.Append("<td rowspan="+list.Count+"><a href="+""+"><font face=Arial Narrow size= 14px >"+ BDEmail + "</font></a></td>"); 

     foreach (var item2 in list)
     {
      //the rest of the td
     }
   }
}

修改 还可以使用单独的css文件来设置td&gt;的样式。 on GitHub

答案 1 :(得分:1)

您也可以使用LINQ执行此操作。首先,您按user_ID制作一个列表,第二个列表将是原样(即原始列表)。然后你可以UNION将它们合二为一。以下是您可以实现的样本。

        var list = (from tl in tempList 
                group tl by tl.id into g
                select new 
                { 
                    g.FirstOrDefault().BDName, 
                    index = g.FirstOrDefault().id, 
                    sort1 = g.FirstOrDefault().id, 
                    sort2 = -1, 
                    g.FirstOrDefault().BDEmail 
                }).ToList();
    int i = 0;
    var list2 = (from tl2 in tempList
                 let index = i++
                 select new 
                 { 
                     BDName = "    ", 
                     index, 
                     sort1 = tl2.id, 
                     sort2 = -1, 
                     tl2.BDEmail 
                 }).ToList();

    var listFinal = list.Union(list2.Where(x => x.index > 1))
                    .OrderBy(x => x.sort1)
                    .OrderBy(x => x.sort2);

Here正在工作dotnetfiddle。希望它有所帮助。