如何使用LINQ基于逗号分隔值获取行 [英] How do get the row based on comma separated value using LINQ

查看:91
本文介绍了如何使用LINQ基于逗号分隔值获取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





Sno     User_ID

---      -----------

1       S191,S192,S193,S195

2       S291,S192,S193,SSS

3       S291,S292,S393,S395





我的列col1中有逗号分隔的数据,我有一个单字符串usong LINQ



我的输入是 String str_user_id = S193



必需输出是:我希望那个匹配任何User_id的行进入User_ID列



喜欢



1    S191,S192,S193,S195

2    S291,S192,S193,SSS



这个

提前感谢



我尝试过:



我试过这个,没有得到正确的输出



string prefixMatch = SearchDistCode +,;

string suffixMatch =,+ SearchDistCode;

string innerMatch =,+ SearchDistCode +, ;



var dist_code =(来自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))

选择新的

{

ratebrokcode = r.Rate_Structure_ID

}。ToList();

解决方案

说真的吗?不要那样存放。分别存储每个UserID - 将它们存储为CSV数据只会让您的生活变得困难 - 这就是导致您出现问题的重要部分!等到你移除一个用户......



但是......你可以躲避让这个工作:

< pre lang =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(< span class =code-string>'
,')。包含( < span class =code-string> S193
));


如果你想使用Linq,你可以在很少的情况下实现方式:





  1. 使用其中 + 任何 - 位复杂

     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)。分割( new string [] { },StringSplitOptions .RemoveEmptyEntries).Any(y => y == suserid))
    .Select(x => x)
    .ToList();

    // 返回:
    // 1 | S191,S192,S193,S195
    // 2 | S291,S192,S193,SSS





  2. 使用包含( ) - 更简单的方法

    参见解决方案1 ​​ [ ^ ]由 OriginalGriff [ ^ ]

    但是,请阅读我对该答案的评论。

    。其中(x => x.Field< string>(  User_ID)。包含(idToFind))




< blockquote>参考这篇文章如何查询逗号分隔值列在SQL中 [ ^ ]


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();

解决方案

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:

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"));


If you would like to use Linq, you can achieve that in few ways:


  1. using Where + Any - bit complicated

    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.

    .Where(x=>x.Field<string>("User_ID").Contains(idToFind))



refer this article How to Query Comma Separated Value Column in SQL[^]


这篇关于如何使用LINQ基于逗号分隔值获取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆