2014-11-01 47 views
0

我有這段代碼片段,它從數據集返回特定的記錄。Linq結果到var不會填充值,除非手動刷新

DataTable tblr = new DataTable("itemsToValidate"); 

      tblr.Columns.Add("Position", typeof(string)); 
      tblr.Columns.Add("ItemCode", typeof(string)); 
      tblr.Columns.Add("QtyOrdered", typeof(string)); 
      tblr.Columns.Add("UOM", typeof(string)); 
      tblr.Columns.Add("PriceQuoted", typeof(string)); 
      tblr.Columns.Add("RequiredBy", typeof(string)); 
      tblr.Columns.Add("ExpectedOnDock", typeof(string)); 
      tblr.Columns.Add("BackOrdered", typeof(string)); 

      var records = (from t1 in x12.InterchangeDataSet.Tables["PO1"].AsEnumerable() 
          join t2 in x12.InterchangeDataSet.Tables["DTM"].AsEnumerable() on t1.Field<Nullable<int>>("Loop_Id") 
          equals (t2.Field<Nullable<int>>("Loop_Id")) 
          select tblr.LoadDataRow(new object[]{ 
         //     Position,     ItemCode,    qtyOrdered,    UOM,   PriceQtd,     RequiredBy,   Net,status 
           t1.Field<string>("PO101"),t1.Field<string>("PO109"),t1.Field<string>("PO102"),t1.Field<string>("PO103"),t1.Field<string>("PO104"),t2.Field<string>("DTM02"),null,null },false) 
          ); 
     IEnumerable<DataRow> x = records.Select(y=>y); 
      tblr.AcceptChanges(); 

      DataSet ds = new DataSet(); 
      ds.Tables.Add(tblr); 
      string ItemsXml = ds.GetXml(); 

除非我設置一個破發點後,立即「記錄」並刷新結果來看,該表(tblr)沒有得到填充。 如何在沒有此干預的情況下填充tblr?

P.S:

下面是修改後的代碼片段包括校正由Tim Schmelter建議,請注意,我用的不是的foreach迭代器附加SELECT COUNT語句。

DataTable tblr = new DataTable("itemsToValidate"); 

      tblr.Columns.Add("Position", typeof(string)); 
      tblr.Columns.Add("ItemCode", typeof(string)); 
      tblr.Columns.Add("QtyOrdered", typeof(string)); 
      tblr.Columns.Add("UOM", typeof(string)); 
      tblr.Columns.Add("PriceQuoted", typeof(string)); 
      tblr.Columns.Add("RequiredBy", typeof(string)); 
      tblr.Columns.Add("ExpectedOnDock", typeof(string)); 
      tblr.Columns.Add("BackOrdered", typeof(string)); 

      var records = (from t1 in x12.InterchangeDataSet.Tables["PO1"].AsEnumerable() 
          join t2 in x12.InterchangeDataSet.Tables["DTM"].AsEnumerable() on t1.Field<int?>("Loop_Id") 
          equals (t2.Field<int?>("Loop_Id")) 
          select tblr.LoadDataRow(new object[]{ 
         //     Position,     ItemCode,    qtyOrdered,    UOM,   PriceQtd,     RequiredBy,   Net,status 
           t1.Field<string>("PO101"),t1.Field<string>("PO109"),t1.Field<string>("PO102"),t1.Field<string>("PO103"),t1.Field<string>("PO104"),t2.Field<string>("DTM02"),null,null },false) 
          ); 
      var x = (from n in records select n).Count(); 

回答

2

Select延遲執行,您必須使用foreach或執行查詢的其他方法,如Count。但我只是簡單地使用foreach循環來通過table.Rows.Add而不是LoadDataRow從查詢中添加行。 LINQ查詢不應該導致像添加行那樣的副作用:

var records = from t1 in x12.InterchangeDataSet.Tables["PO1"].AsEnumerable() 
       join t2 in x12.InterchangeDataSet.Tables["DTM"].AsEnumerable() 
       on t1.Field<int?>("Loop_Id") equals t2.Field<int?>("Loop_Id") 
       select new { 
       PO101 = t1.Field<string>("PO101"), 
       PO109 = t1.Field<string>("PO109"), 
       PO102 = t1.Field<string>("PO102"), 
       PO103 = t1.Field<string>("PO103"), 
       PO104 = t1.Field<string>("PO104"), 
       DTM02 = t2.Field<string>("DTM02") 
      }; 

foreach(var x in records) 
{ 
    DataRow newRow = tblr.Rows.Add(); 
    newRow.SetField(0, x.PO101); 
    newRow.SetField(1, x.PO109); 
    newRow.SetField(2, x.PO102); 
    newRow.SetField(3, x.PO103); 
    newRow.SetField(4, x.PO104); 
    newRow.SetField(5, x.DTM02);  
} 
0

我不是很熟悉ADO.NET(?),但它似乎是你必須列舉了IEnumerable從

records.Select(y=> y); 

返回LINQ可以讓你與像.ToList()方法做到這一點, .First()等。看看LINQ懶惰評估。