James Curran

I'm having trouble with the following code:

Code Block

static void Main(string[] args)
{
Test1DataContext db1 = new Test1DataContext(); // Has Table Tbl1
Test2DataContext db2 = new Test2DataContext(); // Has table Tbl2

var tbl = (from d in db1.Tbl1s
select new // select new Tbl2
{
Col1 = d.ID,
Col2 = d.Fld1,
Col3 = d.Fld2,
Col4 = d.Fld3
}
);
// db2.Tbl2s.AddAll(tbl);

foreach (var e in tbl)
{
Console.WriteLine("{0}, {1}, {2}, {3}", e.Col1, e.Col2, e.Col3, e.Col4);
db2.Tbl2s.Add(e);
}
db2.SubmitChanges();
}

(This is a simplified version of the actual code) My goal is to read the table from one database, build objects in the format used by the other database, and write them to that database.

In the above code, I'd like the use the AddAll, but the foreach should do the same thing. Both were failing in the same way.

Tbl1 has 20 rows in it.

If I create an anonymous class, as shown above (select new {..}) then the WriteLine will show 20 distinct objects. However, the Add() fails, because the object being added in not a Tbl2 object.

If I create the correct objects, as shown in the comment (select new Tbl2 {...}), then the WriteLine will show the first row 20 times. (i.e. for each row in the table, i've given a reference to the first row). The Add() succeeds, but only one row is added to the table.

I've tried adding .ToArray() & .ToList() at the end of the LINQ expression, but the results are the same.

Any Ideas




Re: LINQ Project General DLINQ: Transfering/translating data between databases.

James Curran

If I change the expression to this:

Code Block

var tbl = (from d in db1.Tbl1s

select new

{

Col1 = d.ModTime.Millisecond,

Col2 = d.ID.ToString(),

Col3 = d.Text.Substring(5, 10),

Col4 = d.ModUser

}

)

.ToList()

.ConvertAll<Tbl2>(e => { return new Tbl2{ Col1 = e.Col1, Col2 = e.Col2, Col3 = e.Col3, Col4 = e.Col4 }; });

db2.Tbl2s.AddAll(tbl);

db2.SubmitChanges();

Then it works as desired, but that does seem to be a needly extra step(s).




Re: LINQ Project General DLINQ: Transfering/translating data between databases.

Dariusz Jankowski

Add code marked as red:

Code Block

static void Main(string[] args)
{
Test1DataContext db1 = new Test1DataContext(); // Has Table Tbl1
Test2DataContext db2 = new Test2DataContext(); // Has table Tbl2

var tbl = (from d in db1.Tbl1s
select new Tbl2 // select new Tbl2
{
Col1 = d.ID,
Col2 = d.Fld1,
Col3 = d.Fld2,
Col4 = d.Fld3
}
);
// db2.Tbl2s.AddAll(tbl);

foreach (var e in tbl)
{
Console.WriteLine("{0}, {1}, {2}, {3}", e.Col1, e.Col2, e.Col3, e.Col4);
db2.Tbl2s.Add(e);
}
db2.SubmitChanges();
}





Re: LINQ Project General DLINQ: Transfering/translating data between databases.

James Curran

I tried that originally (that's why the code is in the comment). It doesn't work. Read the second half of my original message.




Re: LINQ Project General DLINQ: Transfering/translating data between databases.

Wael Farag

Try to declare the Query Result with the Tbl2 instade of var

as the following:

Changed Code

static void Main(string[] args)
{
Test1DataContext db1 = new Test1DataContext(); // Has Table Tbl1
Test2DataContext db2 = new Test2DataContext(); // Has table Tbl2

IEnumerable<Tbl2> tbl = (from d in db1.Tbl1s
select new Tbl2
{
Col1 = d.ID,
Col2 = d.Fld1,
Col3 = d.Fld2,
Col4 = d.Fld3
}
);
db2.Tbl2s.AddAll(tbl);

foreach (var e in tbl)
{
Console.WriteLine("{0}, {1}, {2}, {3}", e.Col1, e.Col2, e.Col3, e.Col4);
// db2.Tbl2s.Add(e);
}
db2.SubmitChanges();
}