Click here to Skip to main content
15,886,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I have two lists.

one list is for center where i have cities(ID's) as item(separated by commas).

another list is for session where i have only city(ID) as item.

now i want to compare session city with center cities.

session will have only one city, like: hyd.

Where,
Center will have cities, like: Delhi, Mumbai, Hyd...

i want to compare session where session-city == center-city

What I have tried:

i tried using contains, findall, where, select...
SessionController.Instance.All.FindAll(
item => 
item.ProgramID == programId && /* Resource Path : Program */
//item.SectorID.Contains(item.SectorID) && /* Resource Path : Sector */
//item.CenterGroup.CenterIDs.Split(',').ToList().Contains(center.ID.ToString())
item.CityID==cityIds)//error.
Select(item => new { id = item.ID, value = item.NameEN })


plz guys, help me, how can i compare.

Thanks
Posted
Updated 19-Sep-16 5:06am
v2
Comments
Karthik_Mahalingam 19-Sep-16 8:04am    
what is cityIds? and item.CityID
abdul subhan mohammed 19-Sep-16 10:19am    
cityid contains list of city id's and where item.cityid contain single cityid
Maciej Los 19-Sep-16 11:00am    
What kind of data is CityId (int? string?)?

Based on example provided by OriginalGriff[^], i'd share another solution, which uses Where + Any Linq methods:

var found = session
    .Where(s => centers
        .SelectMany(c => c.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries))
        .Any(z=>z==s));


In case when CityId is type of Int, you need to convert splited string to integer:
.Any(z=>Convert.ToInt32(z)==s));


For further details, please see:
String.Split Method (Char[], StringSplitOptions) (System)[^]
How to: Parse Strings Using String.Split (C# Programming Guide)[^]
Enumerable.Any(TSource) Method (IEnumerable(TSource), Func(TSource, Boolean)) (System.Linq)[^]
 
Share this answer
 
v2
If you mean something like:
C#
List<string> centers = new List<string>();
centers.Add("123,456,789");
centers.Add("666,333,999");
List<string> session = new List<string>();
session.Add("456");
session.Add("666");
session.Add("888");
Then try:
C#
var found = session.Where(s => centers.Where(c => c.Split(',').Contains(s)).Count() != 0);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900