Linq结果为var不会填充值,除非手动刷新

时间:2014-11-01 22:45:34

标签: c# linq .net-4.5

我有这个代码片段,它返回数据集中的特定记录。

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建议的修正,注意我使用了额外的select count语句而不是foreach迭代器。

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 个答案:

答案 0 :(得分: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);       
}

答案 1 :(得分:0)

我对ADO.NET(?)不太熟悉,但似乎你必须枚举从

返回的IEnumerable
records.Select(y=> y);

LINQ允许您使用.ToList().First()等方法来执行此操作。请查看LINQ惰性求值。