SolvedPrivate

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

Posted on 2014-09-23
2
57 Views
Last Modified: 2016-02-16
I would like to  use LINQ in C# to filter a list of objects using values from a string array. How can i get the list where it only returns the objects that match values in my String Array?


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

var newlist = DataObject.where(x => x.names.Contains("any names from my string Array");


note; The Names is a delimited list
0
Comment
Question by:bojeff30
[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
2 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40339359
You would need something like:

			List<string> namesArray = new List<string>() { "Bob", "Joe", "Dan", "Rick" };
			List<ComplexData> DataObject = new List<ComplexData>();
			DataObject.Add(new ComplexData() { Account = 1, City = "Ramhurst", Names = "Rick,Peter,Paul" });
			DataObject.Add(new ComplexData() { Account = 2, City = "Paulson", Names = "David,Dan,Daryl" });
			DataObject.Add(new ComplexData() { Account = 3, City = "Petersburg", Names = "Nuk,Nit,Nat" });
			DataObject.Add(new ComplexData() { Account = 4, City = "Salsburg", Names = "Joe,Diane,Tracy" });
			DataObject.Add(new ComplexData() { Account = 5, City = "Templeton", Names = "Larry,Nancy,Bob" });
			var newList = (from item in DataObject
						from name in namesArray
						where item.Names.Contains(name)
						select item);
			foreach (var item in newList)
			{
				MessageBox.Show(string.Format("Account - {0}; City - {1}; Names - {2}", item.Account, item.City, item.Names));
			}

Open in new window


ComplexData class:
	public class ComplexData
	{
		public string Names { get; set; }
		public string City { get; set; }
		public int Account { get; set; }
	}

Open in new window


HTH,

-saige-
0
 

Author Comment

by:bojeff30
ID: 40339599
Thank you so much! This worked perfectly!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article will show, step by step, how to integrate R code into a R Sweave document
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

623 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