Solved

Linq to find matching record

Posted on 2013-12-13
9
487 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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