Solved

Linq to find matching record

Posted on 2013-12-13
9
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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