Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
            };

The error I am receiving is cannot convert type AnonymousType#1 to My Result on the foreach line
Posted

1 solution

You need to do 'foreach var', because that's what LINQ returns, not anything strongly typed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900