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
53 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

751 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