English 中文(简体)
在LINQ中找到类似记录
原标题:Finding similar records in LINQ

我有以下准则查询,用于发现任何类似货物:

from c in cons
group c by new { c.TripDate.Value, c.DeliveryPostcode, c.DeliveryName } into cg
let min = cg.Min(a => a.DeliverFrom)
let max = cg.Max(a => a.DeliverFrom)
let span = max - min
where span.TotalMinutes <= 59
select cg;

The main thing is the min, max and span. Basically, any consignments that are in the group , that have a DeliverFrom datetime within 59 minutes of any other one in the group, will be returned in the group.

上述法典最初对我很好,但经过进一步检查,如果小组的记录超过2份,“一体行动,履行使命”各有59分钟,“履行使命”日期在59分钟之内有5分钟,那么问询不会再回这个群体,因为要选择min子和max子,看不到59分钟。 我要看到的是,“交货日期”小组中有2批货物已接近尾声,而只是选择一个含有两批货物的集团。

我怎么做呢?

EDIT: Doh, 增加了另一个条款。 有一个称为“光”和“空间”的田地,每一组空间最多可达到26秒和26天。

最佳回答

I d group the consignments with the same logic like you do but use this overload of GroupBy instead, allowing me to project each group of consigments into another type. This type would here be an enumerable sequence of groups of consigments, each element in which represents consignments that non only were in the same group to begin with, but also should all be delivered within the duration of an hour. So the signature of resultSelector would be

Func<anontype, IEnumerable<Consignment>, IEnumerable<IEnumerable<Consignment>>>

At this point it becomes clear that it would probably be a good idea to define a type for the grouping so that you can get rid of the anonymous type in the above signature; otherwise you d be forced to define your resultSelector as a lambda.

resultSelector内,你首先需要按DeliverFrom将一批货物运入,然后根据该时间返回分组。 因此,它可能认为:

IEnumerable<IEnumerable<Consignment>>
Partitioner(ConsignmentGroupKey key, IEnumerable<Consignment> cg)
{
    cg = cg.OrderBy(c => c.DeliverFrom);
    var startTime = cg.First().DeliverFrom;
    var subgroup = new List<Consignment>();

    foreach(var cons in cg) {
        if ((cons.DeliverFrom - startTime).TotalMinutes < 60) {
            subgroup.Add(cons);
        }
        else {
            yield return subgroup;
            startTime = cons.DeliverFrom;
            subgroup = new List<Consignment>() { cons };
        }
    }

    if (subgroup.Count > 0) {
        yield return subgroup;
    }
}

我没有这样做,但只要我能够告诉它应该工作。

问题回答

如果我不犯错,你所期待的是一个统计问题,即:cluster identification,如果是,这是一个比你认为更为复杂的问题。

想象你们有3个条目,时间为1、1:30和2:00。 你们怎样才能把这些问题归类? 头两个或最后两个组织要么是作为一个集团工作(不到59分钟),但所有三个组织都不工作。

如果你只想把链条项目加在一起,只要它们在集团内任何其他项目的59分钟之内,那么你就必须保持循环,直到你不再寻找新的项目来补充任何组别。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签