I have two Entity Framework Models which are identical in structure, they were generated using Code First from Oracle DB (2 "twin" tables, please don't ask why, I don't have control over DB).
I was looking for an efficient way to do it with Linq, trying to avoid rewriting same queries twice.
Some examples:
[Table("ORACLE.TABLE1")]
public partial class MyModel1
{
[Key]
[Column(Order = 0)]
[StringLength(3)]
public string field1 { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(3)]
public string field2 { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int field3 { get; set; }
// -- many other fields
}
[Table("ORACLE.TABLE2")]
public partial class MyModel2
{
[Key]
[Column(Order = 0)]
[StringLength(3)]
public string field1 { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(3)]
public string field2 { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int field3 { get; set; }
// -- many other fields, same as above
}
Somewhere, I have:
public virtual DbSet<MyModel1> MyModel1s { get; set; }
public virtual DbSet<MyModel2> MyModel2s { get; set; }
My current trivial and ugly solution for getting data is this:
var result1 = from t in db.MyModel1s
where t.field1 == myparameter1
&& t.field2 == myparameter2
&& t.field3 == myparameter3
select t;
var result2 = from t in db.MyModel2s
where t.field1 == myparameter1
&& t.field2 == myparameter2
&& t.field3 == myparameter3
select t;
var result = result1.ToList();
... And finally ...
foreach (var res2 in result2)
{
var m1 = new MyModel1
{
field1 = res2.field1,
field2 = res2.field2,
field3 = res3.field3
};
result.Add(m1);
}
Now result
contains all that I want.
There must be a better way, suggestions?