English 中文(简体)
与Linq的搜索
原标题:Searching with Linq

我有一批物体,每个物体都有一片骨肉。 鉴于情况暗淡,我想在最接近的收集中找到目标。

这里,我今天所做的是:

public static void Search(int frameNumber)
{
    var differences = (from rec in _records
                       select new { FrameDiff = Math.Abs(rec.Frame - frameNumber), Record = rec }).OrderBy(x => x.FrameDiff);

    var closestRecord = differences.FirstOrDefault().Record;

    //continue work...
}

这是巨大的,除了我收集的200 000件物品外,我经常使用这种方法。 这样做是否有相对容易、更有效率的途径?

最佳回答
var closestRecord = _records.MinBy(rec => Math.Abs(rec.Frame - frameNumber));

http://code.google.com/p/morelinq/“rel=“nofollow noreferer”>MoreLINQ。

问题回答

您不妨尝试的是,将框架储存在一个按<代码>分类的数据结构中。 Frame。 然后,当你需要找到最接近某个基准区时,你可以进行双双轨搜查。

我不知道,我会为此使用准则,至少不用按顺序。

static Record FindClosestRecord(IEnumerable<Record> records, int number)
{
    Record closest = null;
    int leastDifference = int.MaxValue;

    foreach (Record record in records)
    {
        int difference = Math.Abs(number - record.Frame);
        if (difference == 0)
        {
            return record; // exact match, return early
        }
        else if (difference < leastDifference)
        {
            leastDifference = difference;
            closest = record;
        }
    }

    return closest;
}

你可以把发言合并成一席:

var closestRecord = (from rec in _records
                   select new { FrameDiff = Math.Abs(rec.Frame - frameNumber), 
                   Record = rec }).OrderBy(x => x.FrameDiff).FirstOrDefault().Record;

您能否将贵重项目清单分为五、十小小小名单,这些名单是由其分裂主义者或某些人下令的?

如果你知道哪一个名单需要找寻,这种搜查就会更快。





相关问题
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. ...

热门标签