问题描述
我是LINQ的新手,正在努力将我的数据写出来.
我有2组传入数据,一组来自SQL表,另一组来自XML文件.这些我
在对记录进行一些比较之后,使用Linq进行联合.
然后,我需要将比较数据写入另一个数据集或直接写入XML.由于
变量的匿名性质我每时每刻都在接受错误,现在正在挣扎.
任何建议表示赞赏.代码如下:
Hi, I am new to LINQ and am struggling to write my data out.
I have 2 sets of incoming data, one from a SQL table the other from an XML file. These I
union using Linq, after doing some comparison between the records.
I then need to write the comparison data to another dataset or direct to XML. Due to the
anonymous nature of the variables I am receiving errors at every turn and am now struggling.
Any advice is appreciated. The code is as follows:
DataSet dsPDADocs = new DataSet();
dsPDADocs.ReadXml(path, XmlReadMode.ReadSchema);
dsPDADocs.AcceptChanges();
conCon.Open();
SqlDataAdapter daDBDocs = new SqlDataAdapter("Select * from Documents (nolock)", conCon);
DataSet dsDBDocs = new DataSet();
daDBDocs.FillSchema(dsDBDocs, SchemaType.Source, "Documents");
daDBDocs.Fill(dsDBDocs, "Documents");
var orig = dsPDADocs.Tables[0].AsEnumerable();
var updated = dsPDADocs.Tables[0].AsEnumerable();
//First, get records to delete if any
var delRec = from u in orig where !(from o in orig
select o.Field<Guid>("DocumentID"))
.Contains(u.Field<Guid>(" DocumentID"))
select new
{
prim_key = u.Field<Guid>("DocumentID"),
field1 = u.Field<Guid>("SourceID"),
field2 = u.Field<Guid>("DocumentTypeID"),
field3 = u.Field<object>("DocumentSource"),
field4 = u.Field<string>("DocumentFilename"),
field5 = u.Field<int>("DocumentVersion"),
rec_type = "D"//Deleted
};
//Secondly, getting updated records
var updRec = from o in orig
join u in updated
on o.Field<Guid>("DocumentID")
equals u.Field<Guid>("DocumentID")
where (o.Field<Guid>("SourceID") !=
u.Field<Guid>("SourceID")) ||
(o.Field<int>("DocumentVersion") !=
u.Field<int>("DocumentVersion"))
select new
{
prim_key = o.Field<Guid>("DocumentID"),
field1 = o.Field<Guid>("SourceID"),
field2 = o.Field<Guid>("DocumentTypeID"),
field3 = o.Field<object>("DocumentSource"),
field4 = o.Field<string>("DocumentFilename"),
field5 = o.Field<int>("DocumentVersion"),
rec_type = "M"//Mofified
};
//Third get records to add, if any
var addRec = from o in orig
where !(from u in updated
select u.Field<Guid>("DocumentID"))
.Contains(o.Field<Guid>(" DocumentID"))
select new
{
prim_key = o.Field<Guid>("DocumentID"),
field1 = o.Field<Guid>("SourceID"),
field2 = o.Field<Guid>("DocumentTypeID"),
field3 = o.Field<object>("DocumentSource"),
field4 = o.Field<string>("DocumentFilename"),
field5 = o.Field<int>("DocumentVersion"),
rec_type = "A"//Added
};
var Union = addRec.Union(updRec).Union(delRec);
//DataTable dtResults = Union.ToADOTable(rec => new object[] { Union });
string fileName = "Manifest.xml";
foreach(MyResult dr in Union)
//foreach(DataRow dr in dtResults)
{
XElement Documents =
new XElement("Document",
new XElement("DocumentID",dr.DocumentID),
new XElement("SourceID", dr.SourceID),
new XElement("DocumentTypeID", dr.DocumentTypeID),
new XElement("DocumentSource", dr.DocumentSource),
new XElement("DocumentFilename", dr.DocumentFilename),
new XElement("DocumentVersion", dr.DocumentVersion),
new XElement("rec_type", dr.rec_type));
Documents.Save(fileName);
};
我收到的错误是无法在foreach行上将类型AnonymousType#1转换为My Result
The error I am receiving is cannot convert type AnonymousType#1 to My Result on the foreach line
推荐答案
这篇关于尝试将LINQ查询写入XML或数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!