Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI guys,

I have list, in which i have column with multiple integer values(separated by commas).
eg:
------------------------
| Program | Module      |
------------------------
| I       | 1,2,3       |
------------------------
| II      | 2, 3        |
------------------------
| III     | 1,3         |
------------------------


Now, supppose, if i want to select programs where module is 2.

What I have tried:

int sid = 2;
IEnumerable<int> ids = Program.All.FindAll( item => sid.contains(item.Module.Split(',').Select(s => (int)s) // getting error: cannot convert string to int


Can any one plz help me.


Thanks
Posted
Updated 12-Jul-16 22:55pm
v2
Comments
F-ES Sitecore 13-Jul-16 4:48am    
This is what happens when you don't structure your database correctly. The proper solution is to redesign your database to use one-to-many relationships.
abdul subhan mohammed 13-Jul-16 4:51am    
any solution for this?
F-ES Sitecore 13-Jul-16 4:52am    
Redesign your database tables, google for how you implement a one-to-many relationship in SQL, then google how to use a one-to-many relationship in LINQ.
abdul subhan mohammed 13-Jul-16 4:55am    
any solution without redesigning the database?

1 solution

try this

C#
List<Prog> lst = new List<Prog> ();
     lst.Add( new Prog(){ Program_ = "I", Module =  "1,2,3"});
     lst.Add( new Prog(){ Program_ = "II", Module =  "2,3"});
     lst.Add( new Prog(){ Program_ = "III", Module =  "1,3"});


     int find = 2;

   IEnumerable<string> programs =   lst.Where(k => k.Module.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(a => Convert.ToInt32(a)).Contains(find)).Select(x => x.Program_).ToList();


C#
public class Prog
  {
      public string Program_ { get; set; }
      public string Module { get; set; }
  }
 
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