• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

匹配linq有逗号分隔的多个整数值的列

用户头像
it1352
帮助1

问题说明

HI伙计们,



我有列表,其中我有多个整数值的列(以逗号分隔)。

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         |
------------------------





现在,如果我想选择模块为2的程序,请说明。



我尝试了什么:





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

正确答案

#1
试试这个



try this

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







public class Prog
  {
      public string Program_ { get; set; }
      public string Module { get; set; }
  }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkjhe
系列文章
更多 icon
同类精品
更多 icon
继续加载