Solved

Linq to find matching record

Posted on 2013-12-13
9
494 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 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