Go Premium for a chance to win a PS4. Enter to Win

x
?
SolvedPrivate

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

Posted on 2014-09-22
5
Medium Priority
?
54 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
4 Comments
 
LVL 41

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 41

Accepted Solution

by:
Kyle Abrahams earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

772 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