C#List <list <string>&gt;使用LinQ过滤</list <string>

时间:2014-03-20 20:09:37

标签: c# linq

我有一个List&lt; List&lt; string&gt;&gt;并需要过滤内部列表。它看起来像这样:

{["/DownloadFile2.aspx?File=1026704PR20131001.PDF","10/1/2013 | Monthly Memo","10/1/2013","","","CI","","","","","",""],
 ["/DownloadFile2.aspx?File=1026704PR20141001.PDF","10/1/2014 | Monthly Memo","10/1/2014","","","CC","","","","","",""],
 ["/DownloadFile2.aspx?File=1026704date20130928.PDF","9/30/2013 | New Memo","9/30/2013","","","CC","","","","","",""],
 ["/DownloadFile2.aspx?File=1026704date20140928.PDF","9/30/2014 | New Memo","9/30/2014","","","CI","","","","","",""]}

如何使用LINQ .Where子句过滤第二列?

喜欢过滤术语&#34;每月&#34;会回来:

{["/DownloadFile2.aspx?File=1026704PR20131001.PDF","10/1/2013 | Monthly Memo","10/1/2013","","","CI","","","","","",""],
 ["/DownloadFile2.aspx?File=1026704PR20141001.PDF","10/1/2014 | Monthly Memo","10/1/2014","","","CC","","","","","",""]}

添加代码

我的网络服务的档案定义:

[DataContract(Namespace = "http://www.mysite.com")]
public class Archive
{
    [DataMember]
    public List<string> Header {get; set;}

    [DataMember]
    public List<List<string>> Rows{get; set;}
}

从服务中对存档进行代码设置(mps是Web服务)

/// param contains values used to retrieve a partial set
/// as well as other values used to pass data to jQuery DataTable
///
Archive docs = mps.GetArchiveArray(LogonTicket, PID, param.iDisplayStart, param.iDisplayLength, "D");

List<List<string>> filteredRows;

// If the params sSearch value is not null or empty, filter the results
// else return the full results
if (!string.IsNullOrEmpty(param.sSearch))
{
    // Here's where I need to filter the result set based on the params.sSeach value.
    filteredRows = docs.Rows.?????;
}
else
{
    filteredRows = docs.Rows;
}

然后,使用StringBuilder将filteredRows传递给循环以构建我的JSON,然后将完整的所需结果作为JSON发送:

string result =  sb.ToString();
return Json(new
{
    sEcho = param.sEcho,
    iTotalRecords = numDocs,
    iTotalDisplayRecords = numDocs,
    aaData = result
},
JsonRequestBehavior.AllowGet);

3 个答案:

答案 0 :(得分:3)

在外部列表中使用Select子句,如下所示:

outerList.Select(inr => inr.Where(...))

答案 1 :(得分:3)

var lists = ...;
var filteredLists = (from list in lists
                     where list[1].EndsWith(" | Monthly Memo")
                     select list).ToList();

或者只是它包含Monthly然后

var lists = ...;
var filteredLists = (from list in lists
                     where list[1].Contains("Monthly")
                     select list).ToList();

或者大写/小写不重要

var lists = ...;
var filteredLists = (from list in lists
                     where list[1].ToLower().Contains("monthly")
                     select list).ToList();

在介绍ToList时,lambda语法可能更清晰:

var filteredLists = lists.Where(list => list[1].Contains("Monthly"))
                         .ToList()

编辑如果要检查所有列:

var filteredLists = lists.Where(list => list.Any(s => s.Contains("Monthly")))
                         .ToList()

答案 2 :(得分:0)

使用此(可行):

var listsWithMonthly = listOfLists.Where(x => x.Any(subx => subx.Contains("Monthly"))).ToList();

仅对内部列表中的第二列进行过滤,这也适用:

var listsWithMonthly = listOfLists.Where(x => x[1].Contains("Monthly")).ToList();