Convert Linq Query Result to Dictionary?

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 ).

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 ).

– GAPS Feb 8 at 13:29 @pawan, it's a placeholder for each element in the enumeration and takes on the type of the objects in the enumeration. – tvanfosson Feb 8 at 13:36 @ tvanfosson can you explain me it one. I am not able to Understand.

Now my requirment is to Convert A Linq to Dictionary. Problem is Value is a String (creating in LINQ) format and key is p """var serverUrl = (from p in listi. ProjectName select{ ("tcp://" + listi.

BuildMachineName + ":" + listi. PortNumber+ "/CruiseManager. Rem", p)}""" – GAPS Feb 8 at 13:51 @pawan - that doesn't look right.

I'd expect var servers = list. Select( s => new { s. ProjectName, Url = "tcp://" + s.

BuildMachineName + ":" + s. PortNumber + "/CruiseManager. Rem" } ).

ToDictionary( s => s. ProjectName, s. Url ); This creates a dictionary keyed by project name of project name/url pairs.

– tvanfosson Feb 8 at 14:30 @tvanfosson.... thanx, your answer was not my solution but it work me like rainin drought... thanku – GAPS Feb 87 at 6:22.

Looking at your example, I think this is what you want: var dict = TableObj. ToDictionary(t => t. Key, t=> t.

TimeStamp).

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 don't get the hit of the whole object. I'll keep you posted. – Tipx Jun 5 '09 at 2:10 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 Jun 5 '09 at 2:20 2 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 Jun 5 '09 at 2:25 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 Jun 5 '09 at 2:40 Nice and simple - works like a charm! – Pure.

Krome Jun 5 '097 at 9:01.

Try the following Dictionary existingItems = (from ObjType ot in TableObj). ToDictionary(x => x. Key); Or the fully fledged type inferenced version var existingItems = TableObj.

ToDictionary(x => x. Key).

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 Jun 5 '09 at 1:57 @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 Jun 5 '09 at 1:59 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 Jun 5 '09 at 2:04 I just checked the server queries generated by that code and as you probably knew, it gets the whole objects. – Tipx Jun 5 '09 at 2:21.

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions