SolvedPrivate

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

Posted on 2014-09-22
5
18 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

815 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

10 Experts available now in Live!

Get 1:1 Help Now