views:

3260

answers:

3

Hi, I want to add some rows to a database using Linq to SQL, but I want to make a "custom check" before adding the rows to know if I must add, replace or ignore the incomming rows. I'd like to keep the trafic between the client and the DB server as low as possible and minimize the number of queries.

To do this, I want to fetch as little information as required for the validation, and only once at the beginning of the process.

I was thinking of doing something like this, but obviously, it doesn't work. Anyone have an idea?

Dictionary<int, DateTime> existingItems = 
    (from ObjType ot in TableObj
        select (new KeyValuePair<int, DateTime>(ot.Key, ot.TimeStamp))
    )

What I'd like to have at the end would be a Dictionary, without having to download the whole ObjectType objects from TableObject.

I also taught about the following code, but I was trying to find a proper way :

List<int> keys = (from ObjType ot in TableObj orderby ot.Key select ot.Key).ToList<int>();
List<DateTime> values = (from ObjType ot in TableObj orderby ot.Key select ot.Value).ToList<int>();
Dictionary<int, DateTime> existingItems = new Dictionary<int, DateTime>(keys.Count);
for (int i = 0; i < keys.Count; i++)
{
    existingItems.Add(keys[i], values[i]);
}
+4  A: 

Try the following

Dictionary<int, DateTime> existingItems = 
    (from ObjType ot in TableObj).ToDictionary(x => x.Key);

Or the fully fledged type inferenced version

var existingItems = TableObj.ToDictionary(x => x.Key);
JaredPar
Thanks for the anwser JaredPar.I taught about something like that, but I think this would return the whole objects of ObjType type, and Iwant to avoid having to download the whole objects.
Tipx
@Tipx, can you provide some information on what you want to filter on? Adding a filter clause is possible, but I can't tell from your question what's important
JaredPar
All I need to have to know if the "new row" need to be added, be ignored or replace another row is the timestamp of the object.The objects in the DB have plenty of fields I don't need for the validation and I don't want to get the performance hit of getting the whole objects.To keep it simple, I got a table in my BD with 20 columns, 100 000 rows and I'd want to extract a Dictionary using the values of the first 2 columns.
Tipx
I just checked the server queries generated by that code and as you probably knew, it gets the whole objects.
Tipx
+2  A: 

Looking at your example, I think this is what you want:

var dict = TableObj.ToDictionary(t => t.Key, t=> t.TimeStamp);
BFree
Wow...It might be simple as that... Since I'm pretty new to programming, I'll try that and do a little profiling to make sure that under the hood, I dont get the hit of the whole object. I'll keep you posted.
Tipx
I just made my check.Sadly, while getting the TableObj, it fetches all the objects from the db so I end up getting the trafic hit.I also checked the queries that the second way I posted (and wanted to avoid) and they only get the necessary items. Sure it does 2 queries so the server itself have to search twice the tables, but the object mapping is simple enough.
Tipx
You might be able to do:TableObj.Select(t => new { t.Key, t.TimeStamp }).ToDictionary(t => t.Key, t => t.TimeStamp); LinqToSql should be able to notice that you only want two things (from the select) and return them. I'm not sure it's smart enough to dig into the specifics of ToDictionary().
Talljoe
NICE! Here is the resulting query : SELECT [t0].[Key], [t0].[TimeStamp] FROM [TableObj] AS [t0]. I don't want to take the credit for this so go ahead and post that as an anwser! :-P
Tipx
+6  A: 

Since @Talljoe hasn't added his answer from the comment thread, I'm putting it in here so that people can clearly see what solved @Tipx's problem. @Talljoe -- comment here if you do post your answer and I'll delete this one.

var dict = TableObj.Select( t => new { t.Key, t.TimeStamp } )
                   .ToDictionary( t => t.Key, t => t.TimeStamp );
tvanfosson