我有List
个以下对象:
public class DomainTimeErrors
{
public int DomainId { get; set; }
public int JobId { get; set; }
public DateTime? FinishedAt { get; set; }
public string Domain { get; set; }
public string Url { get; set; }
}
如何在Url
属性中获取具有相同值的对象,TimeSpan
中FinishedAt
之间的var list = new List<DomainTimeErrors>
{
new DomainTimeErrors { DomainId = 1, JobId = 1, FinishedAt = "2017-12-01 12:00", Domain = "so.com", Url = "so.com/id=1" },
new DomainTimeErrors { DomainId = 1, JobId = 2, FinishedAt = "2017-12-01 12:55", Domain = "so.com", Url = "so.com/id=1" },
new DomainTimeErrors { DomainId = 1, JobId = 3, FinishedAt = "2017-12-01 13:55", Domain = "so.com", Url = "so.com/id=1" },
new DomainTimeErrors { DomainId = 1, JobId = 4, FinishedAt = "2017-12-01 14:00", Domain = "so.com", Url = "so.com/id=1" },
new DomainTimeErrors { DomainId = 1, JobId = 11, FinishedAt = "2017-12-01 12:00", Domain = "so.com", Url = "so.com/id=2" },
new DomainTimeErrors { DomainId = 1, JobId = 12, FinishedAt = "2017-12-01 12:05", Domain = "so.com", Url = "so.com/id=2" },
new DomainTimeErrors { DomainId = 1, JobId = 13, FinishedAt = "2017-12-01 12:55", Domain = "so.com", Url = "so.com/id=2" },
new DomainTimeErrors { DomainId = 1, JobId = 14, FinishedAt = "2017-12-01 12:56", Domain = "so.com", Url = "so.com/id=2" }
}
值超过1小时。
编辑:
从以下列表中:
Url = "so.com/id=1"
我想只获得$('#add_name').val()
中的一个,因为最早和最晚之间的时差超过1小时
答案 0 :(得分:1)
使用Linq查询我希望它看起来像
from a in List
join b in List
on a.Url equals b.Url
where (b.FinishedAt - a.FinishedAt).TotalHours>=1
select b;
然后,您将使用分组和选择器来筛选出不需要的值
即
from a in List.Where(i=>i.FinishedAt.HasValue)
join b in List.Where(i=>i.FinishedAt.HasValue)
on a.Url equals b.Url
where (b.FinishedAt - a.FinishedAt).TotalHours>=1
group b by b.url into g
select g.FirstOrDefault();
EDIT 鉴于你的编辑我现在希望它看起来更像这个
from i in List
where i.FinishedAt != null
order by i.FinsihedAt
group i by i.Url into g
where (g.Last().FinsihedAt -g.First().FinsihedAt ).TotalHours >=1
select g.Last();
答案 1 :(得分:1)
如果您使用Linq to Object,我认为这个linq的查询会回答您的问题:
Func<IEnumerable<DomainTimeErrors>, bool> checkdiffrent = (items) =>
{
var itemsCount=items.Count();
if (itemsCount == 0 || itemsCount == 1)
return true;
var sortedItems=items.OrderBy(a=> a.FinishedAt.Value);
var firstItem=sortedItems.First();
var lastItem= sortedItems.Last();
return (firstItem.FinishedAt- lastItem.FinishedAt).Value.TotalHours > 1;
}
var itemWithFinishDate = DomainTimeErrorsCollection.Where(a=> a.FinishedAt.HasValue).
var result = itemWithFinishDate.GroupBy(i=> i.Url).
Where(sameUrls => checkdiffrent(sameUrls))
.SelectMany(a=> a);