foreach (var item in ListOfevents)
{
var query = Globals.db1.events.Where(p => p.ID == item.ID);
dosomething(query);
}
i want to find out, if some row of type "events" with some PrimaryKey exists in
the database or not.If you know the type at designtime it would be really easy.
But i want to try it a bit dynamic. I tried to accomplish that with expression trees. The code below works, but i cant do that totally dynamic.
How i Can replace the 2 Types "events" to something dynamic, in the 2 lines below
i marked in the code?
What I have tried:
private void Query<T>(List<T> obj)
{
var NewObjName = (from a in Globals.db1.Mapping.GetTables()
where a.RowType.Name == obj.GetType().GetGenericArguments()[0].Name
select a).FirstOrDefault();
Type NewObjType = NewObjName.RowType.Type;
IEnumerable<object> NewIObj = obj as IEnumerable<object>;
var NewObjPkCol = (from c in Globals.db1.Mapping.MappingSource
.GetModel(NewObjType)
.GetMetaType(NewObjType)
.DataMembers
where c.IsPrimaryKey == true
select c).SingleOrDefault();
foreach (var item in NewIObj)
{
var query = Queryable.Where(Globals.db1.events,
Expression.Lambda<Func<DB4880_.events, bool>>(
Expression.Equal(
Expression.MakeMemberAccess(
Expression.Parameter(NewObjType, "p"),
NewObjType.GetProperty(NewObjPkCol.Name)),
Expression.MakeMemberAccess(
Expression.Constant(item),
NewObjType.GetProperty(NewObjPkCol.Name))),
Expression.Parameter(NewObjType, "p")));
}
i want to put in a list<of any table in my database>.
query each row if it exists in the database, if not i insert it.
if exists i update it with the new informations.
in my code above i tried it with a specific table called "events".
my goal would be to have ONE routine to Insert or Update any table in my database
and not for each table a routine.
private void Query<T>(List<T> obj)
{
var NewObjName = (from a in Globals.db1.Mapping.GetTables()
where a.RowType.Name == obj.GetType().GetGenericArguments()[0].Name
select a).FirstOrDefault();
Type NewObjType = NewObjName.RowType.Type;
IEnumerable<object> NewIObj = obj as IEnumerable<object>;
Globals.db1.GetTable(NewObjType).InsertAllOnSubmit(NewIObj);
Globals.db1.SubmitChanges();
}
With that code i can Insert a List<of any="" table=""> into the database,
but if 1 row exists in the database, i get a execption or the sql server creates
a new row in case, how the Primary Key is configurated.
thats the reason why i have to find out at Runtime the primary Key of the table with code below.
var NewObjPkCol = (from c in Globals.db1.Mapping.MappingSource
.GetModel(NewObjType)
.GetMetaType(NewObjType)
.DataMembers
where c.IsPrimaryKey == true
select c).SingleOrDefault();
then query every row over Primary Key in the database with code Below!
you can see i used "events" (table in database) at designtime in CODE,
and that is what i want to replace with a variable that represents the
type of the table, which i want to find out at runtime.
var query = Queryable.Where(Globals.db1.
events,
Expression.Lambda<func><db4880_.>
events, bool>>(
Globals.db1 is my public static declaration of the database connection
DB4880_ is the namespace of the linq to sql object
foreach (var item in NewIObj)
{
var query = Queryable.Where(Globals.db1.events,
Expression.Lambda<Func<DB4880_.events, bool>>(
Expression.Equal(
Expression.MakeMemberAccess(
Expression.Parameter(NewObjType, "p"),
NewObjType.GetProperty(NewObjPkCol.Name)),
Expression.MakeMemberAccess(
Expression.Constant(item),
NewObjType.GetProperty(NewObjPkCol.Name))),
Expression.Parameter(NewObjType, "p")));
}