Solved

Linq to find matching record

Posted on 2013-12-13
9
475 Views
Last Modified: 2016-02-10
Hi guys,

I create a list:
private List<String> lst_CounterfeitParts = new List<string>();

Open in new window


Loaded it with part numbers:

lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select (d.PartNumber)).ToList();

Open in new window


Now the next part so far I have
bool has = lst_CounterfeitParts.Any(

Open in new window


But I cant hook the rest up?

Ideally I want a Boolean with a yes/no as to whether there is an exact match on part number. Additionally it would also be nice to generate a list of any partial matches?

Thanks,
Dean
0
Comment
Question by:deanlee17
9 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 400 total points
ID: 39716500
Something like this (it isn't using LINQ though):
bool has = lst_CounterfeitParts.Any(i.ToLower() == "x12345");  //To find part number x12345

For partial matches you might want a regex expression, depends a bit on what you call a partial match.  (Is part number x12345 a partial match with 34 or only with x12 for example)
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 39716574
Correction to AndyAinscow's example:

bool has = lst_CounterfeitParts.Any(i => i.ToLower() == "x12345");

Open in new window


The lambda was incomplete.
0
 

Author Comment

by:deanlee17
ID: 39716584
Thanks for replies guys,

Andy a part match could be 34 or x12
0
 

Author Comment

by:deanlee17
ID: 39716592
What I basically have is a grid populated by a list, I need to filter that list per key stroke whilst looking for full or partial matches on the part number
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39716621
You could use Contains for a partial match...
searchString = searchString.ToLower()
List<String> partialMatches = lst_CounterfeitParts.Where(i => i.ToLower().Contains(searchString));

Open in new window


...but I think most people would find StartsWith more intuitive.
searchString = searchString.ToLower()
List<String> partialMatches = lst_CounterfeitParts.Where(i => i.ToLower().StartsWith(searchString));

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39716664
@kaufmed.  Thanks, I really don't like writing air code.

As it can be a partial match in the middle then StartsWith isn't of use.  As mentioned earlier you could use contains.  I did suggest using ToLower to make everything the same case, if it was case dependent (a not being the same as A) then you would miss out the ToLower conversion.  I'm not so confident with regex so I won't suggest a regex expression - just consider it for finding a match.  It can be more flexible than using a string function such as Contains.
0
 

Author Comment

by:deanlee17
ID: 39716863
Excellent info thanks guys.

Im getting a slight problem, ive changed the original:

lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select (d.PartNumber)).ToList();

Open in new window


to now be :
lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select (d.PartNumber, d.Manufacturer)).ToList();

Open in new window


I now get the error:

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

Is this a common error?

Thanks,
Dean
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39716874
That is really going away from the original question.

Your problem is the change returns two values whereas the List<string> is only expecting one.
0
 

Author Comment

by:deanlee17
ID: 39716893
Ok will start another post. Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now