日期:2014-05-20  浏览次数:20812 次

for 循环问题

我想要的结果是:

<provence sortId="0" name="北京">
<shop sort="0" name="一号店" address="中国北京" tel="1234567" img="images/map/shop.png" stamp="1" />
</provence>

<provence sortId="1" name="浙江">
<shop sort="0" name="一号店" address="浙江" tel="1234567" img="images/map/shop.png" stamp="1" />
</provence>
</data>
代码:

 BLLC_mapone bo = new BLLC_mapone();
  BLLC_maptow bt=new BLLC_maptow();
  var oneshuj = bo.SelectRecordAll() ;//拿到 1级
  var onequerys = from m in oneshuj select m;
  var kk = bt.SelectRecordAll();
   
  for (int x = 0; x <= (onequerys.Count());x++ )
  {
   
  string id = onequerys.Max(m=>m.Id).ToString();

  var qqs = from m in kk where (m.oneid == int.Parse(id)) select m;//根据一级编号拿到 2级

  Response.Write("<provence sortId=\"" + x + "\" name=\"" + onequerys.Max(m => m.Titles) + "\">");


  for (int i = 0; i <= (qqs.Count()-1); i++)
  {
  Response.Write("<shop sort=\"0\" name=\""+qqs.Max(m=>m.name).ToString()+"\" address=\""+qqs.Max(m=>m.address).ToString()+"\" tel=\""+qqs.Max(m=>m.tel).ToString()+"\" img=\""+qqs.Max(m=>m.img).ToString()+"\" stamp=\""+(i+1)+"\" />");
   
  }

  Response.Write("</provence>");
  }


输出的结果是:

<provence sortId="0" name="深圳">
  <shop sort="0" name="又一家酒店" address="枫林路" tel="135135665" img="../CUFile/179006.gif" stamp="1" />
  </provence>
  
  <provence sortId="1" name="深圳">
  <shop sort="0" name="又一家酒店" address="枫林路" tel="135135665" img="../CUFile/179006.gif" stamp="1" />
  </provence>
  
  <provence sortId="2" name="深圳">
  <shop sort="0" name="又一家酒店" address="枫林路" tel="135135665" img="../CUFile/179006.gif" stamp="1" />
  </provence>

上面的数据是对的,它这么老是输出一条数据呢?
我想根据编号匹配,没有就不输出

------解决方案--------------------
string id = onequerys.Max(m=>m.Id).ToString();
------解决方案--------------------
C# code
你的代码逻辑存在问题:

//首先onequerys.Max(m=>m.Id) 这个是固定的,所以外边的循环,每次循环取到的 id 都是相同的
//则 qqs也是相同的
for (int x = 0; x <= (onequerys.Count());x++ )
  {
    
  string id = onequerys.Max(m=>m.Id).ToString();
  var qqs = from m in kk where (m.oneid == int.Parse(id)) select m;//根据一级编号拿到 2级