|
If you want a replacement for Contains() , you don't need to use Where() ... Any() means "If any member of the list satisfies this condition" (Opposite would be !All() )
bool onList = someList.Any(kvp => kvp.Value.Equals(someStringValue));
|
|
|
|
|
If you have to do that frequently, you should probably reengineer the whole thing.
Another way would be to define a specific class to hold the data and implement the Equals method.
|
|
|
|
|
Try this:
var result = someList.FindAll(delegate(KeyValuePair<Guid, string> q) {return q.Value.Equals(someStringValue);});
You can use Find for single values also.
Its the man, not the machine - Chuck Yeager
If at first you don't succeed... get a better publicist
If the final destination is death, then we should enjoy every second of the journey.
|
|
|
|
|
This is the kind of question and discussion I'd so enjoy seeing more of on this forum, thanks, Nick.
It would be fascinating to know how the different techniques suggested here, including Ennis' suggestion of using a HashTable, compare in terms of speed and efficiency. This discussion on StackOverFlow may be relevant (?): [^]
In this case we don't know whether every string in each KeyValuePair is unique or not, and that could influence the gamut of solutions ?
Here's my suggestion, but I'm no Linq master:
List<KeyValuePair<Guid, string>> kvpList = new List<KeyValuePair<Guid, string>>();
private IEnumerable<string> sList;
private void testKVPToList()
{
for (int i = 1000; i < 2000; i++)
{
Guid guid = new Guid("F4168C51-CEB2-4EBC-B6BF-319B339D" + i.ToString());
kvpList.Add(new KeyValuePair<Guid, string>(guid,(i*5).ToString()));
}
sList = from kvp in kvpList select kvp.Value;
if(sList.Contains("5000")) Console.WriteLine("5000 found");
if(sList.Contains("9995")) Console.WriteLine("9995 found");
}
Whether 'tis better to take a 'one-time' hit and build a List, or to dynamically look-up on-the-fly: that's a good question ! And, if the goal is to build the fastest List to search ? My casual attempt to make a SortedList from a KeyValuePair List resulted in a pig that could not fly, but I'm sure there's a way
I'd certainly go and examine Mehdi Gholam's recent 'Hoot' full-text search be-all-and-end-all project[^], and see what he's doing there, because I'll bet it's as optimized as possible.
You can find some implementations of the Boyer-Moore algorithm here on CP:[^].
"Is it a fact - or have I dreamt it - that, by means of electricity, the world of matter has become a great nerve, vibrating thousands of miles in a breathless point of time? Rather, the round globe is a vast head, a brain, instinct with intelligence!" - Nathanial Hawthorne, House of the Seven Gables
modified on Thursday, September 8, 2011 2:45 AM
|
|
|
|
|
Thanks! That's good information you provided. Overall I am pleasantly surprised and pleased with the responses to my initial questions. Everyone provided some good ideas and I even learned a couple new things concerning LINQ.
This issue started out as a minor client-requested enhancement to one of their existing applications (hopefully just a one-time deal), and when I looked at the code in question I discovered the data I needed to compare against already existed in a List of KeyValuePairs, so really no need to query the database again. And thus my little quest began. When I saw the first response from Pete O'Hanlon, I thought: "Of course. I'll just cast the List to an equivalent Dictionary object and use its 'ContainsValue' method." Which I thought was going to work because it compiled just fine, but then threw an 'Invalid Cast' exception during runtime (same thing for a HashTable). And so the quest continued...
I eventually went with the answer provided by MDL=>Moshu. It works great. The List has about 2000 KeyValuePairs in it and the new search appears instantaneous to the end users. So though the code design may not be ideal, it works, it's stable, it performs well, and the client is happy. And that is what it is really all about.
-NP
Never underestimate the creativity of the end-user.
|
|
|
|
|
I am working with a C#.net 2008 console/windows form application that has imbedded third components. The third part components are written with visual studio.net 2005 and possibly some other language.
My question is, do you know of some why I can see if the third party components will expire at somec point? Basically is there something I can look at to see if those components will stop executing at some point? If so, can you point me to a how to determine what the expiration date is and/or point me to a url I can use as a reference?
|
|
|
|
|
I've not heard of DLLs expiring. The onyl way i can see you having a problem in the future in this regards is if the license to use those DLLs expires.
For that you will need to check the license under which you are using the DLLs
Pedis ex oris
|
|
|
|
|
How do I check the license under where the DLLs are used?
|
|
|
|
|
The license isn't generally a part of the DLL.
When you bought the 3rd party library you will have done so with a license agreement, this would be documented either as part of the install package in a manual or on the provider's website.
Essentially, DLLs don't expire; your legal right to use them might and some providers might enforce that rule through external logic within the install package (such as when your free-trial expires). 'If' and 'how' a provider enforces any time constraints varies by provider.
Pedis ex oris
|
|
|
|
|
Lets look at the problem form a different angle.
One must indeed keep track of the third-party user-agreement of the dll's.
But one must also think about where your api wil be used, in which environment.
for ex.
if one is developing for win 95 and the dll's are only for .net environments, one shall encouter a problem.
So when does an dll expire.
a) when the third-party agreement is at an end.
b) when the third-patry no longer supports the dll's (no upgrads)
c) when the end-user changes his/her computing environment.
d) when the developer changes his/her computing environment.
Kribo
htt://blog.kribo.be
|
|
|
|
|
hi there!
i have a big problem with memory releasing in c#, let me explain u my case:
i have a sql class (sqlconnection+ sqlDataAdapter )which fill a DataTable that contains about 34000 rows, now the memory consumtion goes up about twice than before query executed! but the problem is when user closes the host form, memory would not be released compeletly! so after 3 or 4 time execution, application stopped working and out of memory exception!
I tried this solutions: Disposing Table- GC.Finalize- GC.Collect and so but they wont return whole of memory...
plz HELPPPPP
|
|
|
|
|
|
Well, it sounds like your DataTable can't be garbage collected. That indicates that you have a hard reference to it from outside the form that the user closed.
|
|
|
|
|
Learn how to use google:
Investigating Memory Issues[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Garbage collection is probably not the answer to your question. Something is most likely holding on to your references, making it impossible to collect as garbage, even if you call Dispose().
I would use a memory profiler to find out. CLR profiler is free and easy to use. It can be found at Microsofts website: http://www.microsoft.com/download/en/details.aspx?id=16273[^]
//daniel
|
|
|
|
|
Hi,
I want to understand the concept of the Join method.
When I use Thread1.Join() does it mean that Thread1 and the main thread runs simultaneously?
If not, I would be happy if you suggest me other method to do it
Thank from advance!
|
|
|
|
|
Thread.Join has nothing to do with making multiple threads run together - you can think of it as a mechanism where a parent thread can join to child threads and wait until they are finished. It blocks the calling thread and waits for the other threads to finish before it continues processing.
So, why would you want to do this? Well, if your parent thread completes processing faster than it's child threads then there would be trouble if the parent thread terminated leaving the child threads running, if those child threads had to access something on the parent thread.
There is no mechanism for guaranteeing that two threads will run simultaneously - the OS is responsible for allocating threads, your application must respect the rules given to it. Plus, of course, you can only have two or more threads running together if you've got a multi-core machine.
|
|
|
|
|
Are thre Mutex's that can be used in Silverlight?
It seems the Mutex examples I have seen work in window forms but not in silverlight.
In using System.Threading there does not seem to be a Mutex class in Silverlight.
|
|
|
|
|
There isn't a native Mutex available in Silverlight. There may be a third party implementation available on the web, but you will habe to search for that.
|
|
|
|
|
There is no support for Mutex in Silverlight.
Too much of heaven can bring you underground
Heaven can always turn around
Too much of heaven, our life is all hell bound
Heaven, the kill that makes no sound
|
|
|
|
|
Hi Everyone,
I am trying to figure out how to parse html line and take from it the $ sign and a the monetary value.
For example:
The price of the book is $15.00 and the price of the computer is $1,299.99 cents.
Thank you.
|
|
|
|
|
Please ask this in the Regular Expressions forum instead. We're trying to get the traffic up there. Forum[^]
|
|
|
|
|
Hi one hi all!
Using the example from , http://www.undisciplinedbytes.com/2010/06/html-5-c-web-sockets-server-and-asp-net-client-implementation/[^]
I have managed to add basic webSocket support to me TCP server engine. As I have very little knowledge in html and interested only on the sever side I came across few questions regarding the implementation of the WebSockets server:
1)Does the buffer of the handshake arriving from the client, always arrives in one chunk?
2)If not, how can I tell when I have all the handshake message?
3)I have change ‘ws’ to ‘wss’ (both in client and server) in order to use the secure option. As
a result the handshake from the client arrives (what seems to be) encrypted, how can
encrypt it? Where is the key?
Thank you all
|
|
|
|
|
I want to create a web service for a chat application which will instead of Request/response model use push model.
Client app will be notified of a new message, user logged in etc.. by the web service.
Does anyone have know where to start any good article with basic knowledge for creating such push web services.
|
|
|
|
|
I certainly don't; push is evil and never works.
My chat system uses pull. I still haven't yet converted it to use a Web Service.
One of the benefits of using pull is that it makes it easier to allow the client to poll at intervals of the user's choosing and to have a "check now" kind of button.
|
|
|
|