Solved

Linq to find matching record

Posted on 2013-12-13
9
480 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 75

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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