I have a local list of thousands of GUID's (over 100k) of records that I need to update.
Now i'm using simple:
foreach(var id in GUIDs)
{
var objToUpdate = dataContext.table.Single(o=>o.id==id);
objToUpdate.val1=...
objToUpdate.val2=...
...
}
dataContext.SubmitChanges();
This solution is very slow. Every time when i call Single method, the whole record is retrieved from DB, (i don't need it, because i'm overwriting all this data except the Primary Key).
Is there any way I could retrieve only two columns that i really need? (Primary key, and another one column)?
When I do something like this:
dataContext.table.Select(o =>
new sqlRecord
{
PrimaryKey = o.PrimaryKey,
AnotherCol = o.AnotherCol
}
);
i get error:
Explicit construction of entity type '{0}' in query is not allowed.
Using a stored procedure is not an option. Setting correct data values is very complex, depending on external resources.
var query = dataContext.table.Where(o => GUIDs.Contains(o.id));
foreach(var objToUpdate in query)
{
objToUpdate.val1 =...;
objToUpdate.val2 = ...;
}
dataContext.SubmitChanges();
this will produce an error, that i'm using over 2100 parameters, while i have tons of GUIDs.
for this, i'm using an alternative:
(from ids in dataContext.fn_getGuidsFromString(String.Join("", GUIDs)) join o in dataContext.table on ids.id equals o.PrimaryKey select o)
where fn_getGuidsFromString is table function in SQL. This is better then using where and Contains.
My problem is that this works too slow. You must know, that in this table there is over 200 columns, some of them are ntext with a lot of data.
dataContext.table.Single(o=>o.id==id);
is about 20 times slower then this (depending on data):
dataContext.table.Single(o=>o.id==id).select(o=>new {o.id, o.anotherCol});
but with this I cannot update a record.
Do you have any suggestions? Regards