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


Sno    User_ID
---    -----------
1       S191,S192,S193,S195
2       S291,S192,S193,SSS
3       S291,S292,S393,S395


I have comma-separated data in my column called col1, and I have an Single String usong LINQ

my input is String str_user_id=S193

Required Output is : I want that row which is match any User_id into User_ID Column

like

1    S191,S192,S193,S195
2    S291,S192,S193,SSS

this
thanks in advance

What I have tried:

I have tried this, not getting the correct output

string prefixMatch = SearchDistCode + ",";
string suffixMatch = "," + SearchDistCode;
string innerMatch = "," + SearchDistCode + ",";

var dist_code = (from r in en.temp_rate_target_schemes_new
where (r.Distributor_Code.StartsWith(prefixMatch) || r.Distributor_Code.Contains(innerMatch) || r.Distributor_Code.EndsWith(suffixMatch) || (!r.Distributor_Code.Contains(",") && r.Distributor_Code == SearchDistCode))
select new
{
ratebrokcode = r.Rate_Structure_ID
}).ToList();
Posted
Updated 28-Aug-16 10:00am

Seriously? Don't store it like that. Store each UserID separately - storing them as CSV data just makes your life difficult - and this is the trivial bit which is causing you a problem! Wait till you get to removing a user...

But... you can "bodge" to get this working:
C#
Dictionary<int, string> data = new Dictionary<int, string>();
data.Add(1, "S191,S192,S193,S195");
data.Add(2, "S291,S192,S193,SSS");
data.Add(3, "S291,S292,S393,S395");
var matches = data.Where(d => d.Value.Split(',').Contains("S193"));
 
Share this answer
 
Comments
Maciej Los 28-Aug-16 16:00pm    
You don't even need to use Split() function.
Where(d=>d.Contains(idToFind)); - should be enough!
See my answer ;)
OriginalGriff 29-Aug-16 6:22am    
The only reason I'd use split is so that S193 doesn't match with these:
"S291,S1934,S2345"
"S291,AS193,S295"
Maciej Los 29-Aug-16 6:28am    
Good point!
Cheers,
Maciej
OriginalGriff 29-Aug-16 6:41am    
I've been there before, my friend! :laugh:
vermavivek 7-May-20 15:32pm    
please let me know, how to check S191,S193 contains
If you would like to use Linq, you can achieve that in few ways:


  1. using Where + Any - bit complicated
    C#
    DataTable dt = new DataTable();
    dt.Columns.Add("Sno", typeof(int));
    dt.Columns.Add("User_ID", typeof(string));
    dt.Rows.Add(new object[]{1, "S191,S192,S193,S195"});
    dt.Rows.Add(new object[]{2, "S291,S192,S193,SSS"});
    dt.Rows.Add(new object[]{3, "S291,S292,S393,S395"});
    
    string suserid="S193";
    
    var result = dt.AsEnumerable()
    		.Where(x=>x.Field<string>("User_ID").Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries).Any(y=>y==suserid))
    		.Select(x=>x)
    		.ToList();
    		
    //returns:
    //1    |    S191,S192,S193,S195
    //2    |    S291,S192,S193,SSS


  2. using Contains() - simpler way
    See solution 1[^] provided by OriginalGriff[^]
    But, please read my comment to that answer.
    C#
    .Where(x=>x.Field<string>("User_ID").Contains(idToFind))

 
Share this answer
 
Comments
avelsamy 31-Aug-16 6:29am    
its working .... thank you MACIEJ
Maciej Los 31-Aug-16 9:26am    
You're very welcome.
 
Share this answer
 
Comments
Maciej Los 28-Aug-16 16:02pm    
Even if OP wants to find Linq solution, this is good enough!

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