2017-01-30 43 views
1

我试图用来自数据库的数据平均填充两个不同的容器。在第一个容器中放置5个位置,在第二个容器中放置6个位置。每次运行此代码时,它都将填充到左侧容器中。 任何想法我错过了什么?将数据库中的数据平等地填充到多个列中?

shopDataContext dc = new shopDataContext(); 
var areas = (from s in dc.Areas select s); 
String content = ""; 
foreach(var a in areas) { 
    if (a.id >= 1 && a.id <= 6) { 
     content += "<div><h3>" + a.Tittle + "</h3>"; 
     var aStores = (from s in dc.Stores where s.areaid == a.id select s); 
     foreach(var s in aStores) { 
     content += "<p>" + s.Tittle + "</p>"; 
     } 
     leftContent.InnerHtml = leftContent.InnerHtml + content; 
    } else { 
     content += "<div><h3>" + a.Tittle + "</h3>"; 
     var aStores = (from s in dc.Stores where s.areaid == a.id select s); 
     foreach(var s in aStores) { 
     content += "<p>" + s.Tittle + "</p>"; 
     } 
     rightContent.InnerHtml = rightContent.InnerHtml + content; 
    } 
    } 
+0

您错过了一些使用片段编辑器'<>'并清理代码时显而易见的末端括号。请点击那个'<>'编辑器并创建一个[mcve] – mplungjan

+0

您确定,您的区域包含一个ID为< 1 or id > 6的物品吗? –

+0

这不是关于你的问题,而是你不需要var areas =(来自dc.Areas select s中的s);只有地区= dc.Areas就够了。 –

回答

0

我猜你要添加的内容在每个循环迭代复位:

... 
foreach(var a in areas) { 
    string content = ""; 
    if (a.id >= 1 && a.id <= 6) { 
     ... 
    } 
} 
.... 

否则,将添加的内容多次。

+0

非常感谢西蒙D.这正是我正在寻找的! – marceloofreire

+0

不客气@marceloofreire –

相关问题