SolvedPrivate

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
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 exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

744 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

14 Experts available now in Live!

Get 1:1 Help Now