LINQ ( Lamda expression)

Hi Experts

The following code works well:

        private IEnumerable<Customer> GetFromATCCustomerAccess(string Id)
        {
            var customerAccesses = _customerAccessContext.GetCustomerAccessesList().Where(cal => String.Equals(cal.Id.Trim(), Id.Trim(), StringComparison.CurrentCultureIgnoreCase) && cal.ExpirationDttm >= DateTime.Now);

Open in new window


could you please help me in telling what could be change if I pass String array (String[] Ids) to this function instead of (String Id)  because the requirement is to compare with string array and not with one string.
Dinesh KumarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You would do something like:
private IEnumerable<Customer> GetFromATCCustomerAccess(IEnumerable<string> IDs)
{
	return (from cal in _customerAccessContext.GetCustomerAccessesList()
		 from id in IDs
		 where String.Equals(cal.Id.Trim(), id.Trim(), StringComparison.CurrentCultureIgnoreCase) && cal.ExpirationDttm >= DateTime.Now
		 select cal).ToList();
}

Open in new window

Example usage -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28674373
{
	class Program
	{
		static List<Customer> customers = new List<Customer>();
		static void Main(string[] args)
		{
			for (int i = 0; i < 50; i++)
				customers.Add(new Customer() { ID = i.ToString(), Name = string.Format("Customer{0}", i) });

			foreach (var customer in customers.GetFromATCCustomerAccess(from i in Enumerable.Range(0, customers.Count - 1) where i % 3 == 0 select i.ToString()))
				Console.WriteLine(customer);

			Console.ReadLine();
		}
	}

	class Customer
	{
		public string ID { get; set; }
		public string Name { get; set; }

		public override string ToString()
		{
			return string.Format("Customer Name: {0}; ID: {1}", Name, ID);
		}
	}

	static class Extensions
	{
		public static IEnumerable<Customer> GetFromATCCustomerAccess(this IEnumerable<Customer> customers, IEnumerable<string> IDs)
		{
			return (from customer in customers
					 from id in IDs
					 where customer.ID.Trim().Equals(id.Trim())
					 select customer).ToList();
		}
	}
}

Open in new window

Produces the following results:Capture.JPG-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lokesh B RDeveloperCommented:
Hi,

Try this



private IEnumerable<Customer> GetFromATCCustomerAccess(string[] Id)
{
          var customerAccesses = _customerAccessContext.GetCustomerAccessesList().Where(cal => Id.Contains(cal.Id) && cal.ExpirationDttm >= DateTime.Now);
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.