[c#]Linq 结果为 var 不填充值,除非手动刷新

标签: Linq C#
发布时间: 2017/2/28 23:13:15
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有此代码fragment从一个数据集返回特定记录。

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:

这里是修改后的代码fragment,包括修正建议由 Tim Schmelter,注意,我使用额外的选择计数语句而不是 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();

解决方法 1:

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);       
}
官方微信
官方QQ群
31647020