我有一个这样的文档:
{
"Document":{
"Principles":[{"text":"Text","history":["Text1","Text2","Text3"]}]
}
}我想搜索包含包含“文本”的历史的所有原则。
我有两个这样的接口:
[Entity]
public interface IDocument
{
string Id{get;}
ICollection<IPrinciple> Principles{get;set;}
}
[Entity]
public interface IPrinciple
{
string Id{get;}
ICollection<string> history{get;set;}
string text{get;set;}
}下面是我所做的:
using(var context=new MyEntityContext(connectionString))
{
var principles=(from p in context.Principles where p.history.Any(h=>h.Contains("Text")) select p).ToList();
}但是我得到了一个不包含原则的列表。
发布于 2016-12-12 16:05:07
如果你试着这样写:
var principles= _context.Principles.Any(p => p.history.Contains("Text"));您将得到以下错误:
NotSupportedException : LINQ-to-SPARQL当前不支持结果运算符“”Any()“”
所以你的猜测是对的,目前brightstarDB似乎不支持Any()操作。
您总是可以用Where和一些调整来替换Any,以获得类似的(但有效的)结果
发布于 2016-12-12 19:59:57
我不认为Any()是问题所在。这个程序运行得很好,从查询中返回了预期的2个原则。我使用的IPrinciple和IDocument接口与您在原始问题中发布的完全相同,查询也与您最初发布的完全相同。
class Program
{
private static readonly string ConnectionString = "type=embedded;storesDirectory=brightstardb;storeName=anytest";
static void Main(string[] args)
{
SetupStore();
QueryStore();
}
private static void SetupStore()
{
if (!Directory.Exists("brightstardb"))
{
Directory.CreateDirectory("brightstardb");
using (var context = new MyEntityContext(ConnectionString)
)
{
var doc1 = context.Documents.Create();
var doc2 = context.Documents.Create();
var doc3 = context.Documents.Create();
var p1 = context.Principles.Create();
p1.History = new List<string> {"Strings", "of", "Text"};
var p2 = context.Principles.Create();
p2.History = new List<string> {"Nothing", "To See Here", "Move Along"};
var p3 = context.Principles.Create();
p3.History = new List<string> { "Another", "Principle containing Text"};
doc1.Principles = new List<IPrinciple> {p1};
doc2.Principles = new List<IPrinciple> {p2};
doc3.Principles = new List<IPrinciple> {p3};
context.SaveChanges();
}
}
}
private static void QueryStore()
{
using (var context = new MyEntityContext(ConnectionString))
{
var principles = (from p in context.Principles where p.History.Any(h => h.Contains("Text")) select p).ToList();
foreach (var p in principles)
{
Console.WriteLine("Found principle: {0}", p.Id);
}
}
}
}https://stackoverflow.com/questions/41086093
复制相似问题