简体   繁体   English

where子句MongoDB和LINQ中的哈希检查方法

[英]Hash checking method in where clause MongoDB and LINQ

I have the collection that contains images and DCT hash for their. 我有包含图像和DCT哈希的集合。 How I can select only similar images using Similarity(long hash, long hashOther) method in LINQ where clause. 我如何使用LINQ where子句中的相似性(长哈希,长hashOther)方法仅选择相似图像​​。
This query is not work: 此查询不起作用:

var results = imageCollection
.AsQueryable()
.Where(x=>ImageHash.Similarity(x.Hash, hash) >= 50)
.ToList();

Serialization error occurs. 发生序列化错误。

Thanks! 谢谢!

Keep in mind that any LINQ query you write against MongoDB must ultimately be translated into an equivalent query in the MongoDB query language. 请记住,您针对MongoDB编写的任何LINQ查询最终都必须转换为使用MongoDB查询语言的等效查询。 The LINQ query you wrote can't be translated to a raw MongoDB query because this part: 您编写的LINQ查询无法转换为原始MongoDB查询,因为这部分内容:

.Where(x=>ImageHash.Similarity(x.Hash, hash) >= 50)

has no equivalent in the MongoDB query language. 没有与MongoDB查询语言等效的语言。

What you will have to do is fetch all the Image _id and Hash values client side and run your Similarity criteria client side. 您将要做的是获取所有Image _id和Hash值客户端,然后运行相似性条件客户端。 Once you have the _id values of all the images that are similar enough you can fetch the images themselves. 获得所有足够相似的图像的_id值后,即可获取图像本身。

Hopefully your query will also include other criteria so that only a subset of the images need to be run through the Similarity criteria client side. 希望您的查询还将包含其他条件,以便仅需要通过相似性条件客户端运行图像的子集。

It would look more or less like this: 看起来或多或少是这样的:

var matchingIds = collection.FindAllAs<BsonDocument>
    .SetFields("_id", "Hash")
    .AsEnumerable() // force client side evaluation of the rest of the query
    .Where(x => ImageHash.Similarity(x["Hash"].AsByteArray, hash) > 50))
    .Select(x => x["_id"]);
var query = Query.In("_id", matchingIds);
var matchingImages = collection.Find(query);
foreach (var image in matchingImages)
{
    // process matching image
}

As you say in your comment, have you tried a POCO class? 正如您在评论中所说,您是否尝试过POCO课程?

public class MyImage
{
 [BsonId]
 public ObjectId id {get;set;}    
 public string Hash {get;set;}

}

var results = imageCollection
 .AsQueryable(MyImage)
.Where(x=>ImageHash.Similarity(x.Hash, hash) >= 50)
.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM