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
46 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
2 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
Thank you so much! This worked perfectly!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

771 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

9 Experts available now in Live!

Get 1:1 Help Now