score:1

since you are looping through the userids(List) the next upcoming items will always be the one as same as the user above.

You can add rowspan to extend to multiple rows

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
     }
   }
}

EDIT Also use seperate css file to style the td > Explanation

score:1

You can do it using LINQ too. First you make a list grouping it by user_ID, second list will be as it is (i.e. the original list). Then you can UNION them into one. Below is the sample of how you can achieve.

        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 is the working dotnetfiddle. Hope it helps.


Related Articles