两个for循环筛选

wozaiztam 2019-04-25 10:22:08

public class Result
{
public string Url { get; set; }
}

public class UnKeyDomain
{
public string domain { get; set; }
}

var resultList = new List<Result>();
resultList.Add((new Result() { Url = "www.google.com" }));
resultList.Add((new Result() { Url = "www.baidu.com" }));
resultList.Add((new Result() { Url = "www.bing.com" }));
resultList.Add((new Result() { Url = "www.yahoo.com" }));

var unKeyDomainList = new List<UnKeyDomain>();
unKeyDomainList.Add((new UnKeyDomain() { domain = "yahoo.com" }));

//我现在要在resultList 中排除掉含有unKeyDomainList 域名的数据,返回:
// resultList.Add((new Result() { Url = "www.google.com" }));
// resultList.Add((new Result() { Url = "www.baidu.com" }));
// resultList.Add((new Result() { Url = "www.bing.com" }));



请问怎么做?
...全文
434 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanking小杆子 2019-05-17
  • 打赏
  • 举报
回复
var s2 = from p in resultList where !(unKeyDomainList.Select(r => r.domain).ToArray().Contains(p.Url)) select p; 没有4楼的好!
nanking小杆子 2019-05-17
  • 打赏
  • 举报
回复
引用 4 楼 bloodish 的回复:

resultList = resultList.Where(s => !unKeyDomainList.Any(ukd => s.Url.Contains(ukd.domain))).ToList();
wozaiztam 2019-04-25
  • 打赏
  • 举报
回复
resultList.Where(i => !i.Url.Contains(unKeyDomainList[0].domain)).ToList();

这边也不止一条
wozaiztam 2019-04-25
  • 打赏
  • 举报
回复
引用 1 楼 liangshunsheng88 的回复:

public class Result
{
public string Url { get; set; }
}

public class UnKeyDomain
{
public string domain { get; set; }
}


class Program
{
static void Main(string[] args)
{
try
{
var resultList = new List<Result>();

resultList.Add((new Result() { Url = "www.google.com" }));
resultList.Add((new Result() { Url = "www.baidu.com" }));
resultList.Add((new Result() { Url = "www.bing.com" }));
resultList.Add((new Result() { Url = "www.yahoo.com" }));

var unKeyDomainList = new List<UnKeyDomain>();
unKeyDomainList.Add((new UnKeyDomain() { domain = "yahoo.com" }));

//我现在要在resultList 中排除掉含有unKeyDomainList 域名的数据,返回:
//resultList.Add((new Result() { Url = "www.google.com" }));
//resultList.Add((new Result() { Url = "www.baidu.com" }));
//resultList.Add((new Result() { Url = "www.bing.com" }));

resultList = resultList.Where(i => !i.Url.Contains(unKeyDomainList[0].domain)).ToList();

foreach (var item in resultList)//打印resultList集合
{
Console.WriteLine(item.Url);
}

Console.WriteLine("执行成功!");
Console.ReadLine();
}
catch
{
Console.WriteLine("执行失败!");
Console.ReadLine();
}
}
}


不好意思,unKeyDomainList不止一条数据,我少写了
demo00001111 2019-04-25
  • 打赏
  • 举报
回复

public class Result
{
public string Url { get; set; }
}

public class UnKeyDomain
{
public string domain { get; set; }
}


class Program
{
static void Main(string[] args)
{
try
{
var resultList = new List<Result>();

resultList.Add((new Result() { Url = "www.google.com" }));
resultList.Add((new Result() { Url = "www.baidu.com" }));
resultList.Add((new Result() { Url = "www.bing.com" }));
resultList.Add((new Result() { Url = "www.yahoo.com" }));

var unKeyDomainList = new List<UnKeyDomain>();
unKeyDomainList.Add((new UnKeyDomain() { domain = "yahoo.com" }));

//我现在要在resultList 中排除掉含有unKeyDomainList 域名的数据,返回:
//resultList.Add((new Result() { Url = "www.google.com" }));
//resultList.Add((new Result() { Url = "www.baidu.com" }));
//resultList.Add((new Result() { Url = "www.bing.com" }));

resultList = resultList.Where(i => !i.Url.Contains(unKeyDomainList[0].domain)).ToList();

foreach (var item in resultList)//打印resultList集合
{
Console.WriteLine(item.Url);
}

Console.WriteLine("执行成功!");
Console.ReadLine();
}
catch
{
Console.WriteLine("执行失败!");
Console.ReadLine();
}
}
}
bloodish 2019-04-25
  • 打赏
  • 举报
回复

resultList = resultList.Where(s => !unKeyDomainList.Any(ukd => s.Url.Contains(ukd.domain))).ToList();

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧