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();
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"
};
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"
};
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"
};
var Union = addRec.Union(updRec).Union(delRec);
string fileName = "Manifest.xml";
foreach(MyResult dr in Union)
{
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);
};
The error I am receiving is cannot convert type AnonymousType#1 to My Result on the foreach line