SolvedPrivate

use LINQ in C# to filter a return database list using values from a string array.

Posted on 2014-09-22
5
17 Views
Last Modified: 2016-02-16
I would like to  use LINQ in C# to filter a return database list using values from a string array. How can i get the list of rows out of my database list, where the rows have matching values in my String Array?


example:
string[] stringArray = { "Bob", "Joe", "Dan", "Rick" };

var newlist = Data.where(x => x.names.Contains("any names from my string Array");
0
Comment
Question by:bojeff30
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40337742
if name is a single value you can do this:
string[] stringArray = { "Bob", "Joe", "Dan", "Rick" };

string searchString = ",";

foreach (string s in stringArray)
 searchString += s + ",");

//searchstring= ",Bob,Joe,Dan,Rick,"

 Data.where(x => searchString.Contains("," + x.Name + ",");

Open in new window


If Names is a delimited list itself that gets a little more complicated.  Let me know.
0
 

Author Comment

by:bojeff30
ID: 40337946
it's delimited list
0
 

Author Comment

by:bojeff30
ID: 40339412
I've requested that this question be deleted for the following reason:

Did not get response I was looking for
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40339413
Just because we don't get it on the first try doesn't mean we aren't willing to work on it.  There were multiple possibilities and so further clarification was needed.

The following should work for you in this case:

string[] stringArray = { "Bob", "Joe", "Dan", "Rick" };

//split on the delimiter in names and then intersect the 2 arrays.
var newlist = Data.where(x => x.names.Split(',').Intersect(stringArray).Count() > 0);

Open in new window

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New Windows 2012 Server with IIS 8.5 installed all webs get 500.19 errors 17 468
Tomcat Ports in Xampp 8 136
Web Data Entry Application 5 87
Find value in LINQ result ? 4 24
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…

920 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

12 Experts available now in Live!

Get 1:1 Help Now