我通过以下方式从数据库获取数据:
curl -X PUT http://127.0.0.1:9200/test_file \ -d '{ "settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 20
},
"custom_ascii_folding": {
"type": "asciifolding",
"preserve_original": true
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter",
"snowball",
"custom_ascii_folding"
]
}
}
} }, "mappings": {
"sample": {
"properties": {
"id": {
"type": "keyword"
},
"title": {
"type": "text",
"term_vector": "yes",
"analyzer": "autocomplete"
}
}
} } }'
curl -X PUT http://127.0.0.1:9200/test_file/sample/1 -d '{"id": "1","title": "Four Beautyful Sun Flower - Episode 01"}'
curl -X PUT http://127.0.0.1:9200/test_file/sample/2 -d '{"id": "2","title": "Four Beautyful Sun Flower - Episode 15"}'
curl -X PUT http://127.0.0.1:9200/test_file/sample/3 -d '{"id": "3","title": "Four Beautyful Sun Flower - Episode 17"}'
curl -X PUT http://127.0.0.1:9200/test_file/sample/4 -d '{"id": "4","title": "Four Beautyful Sun Flower - Episode 23"}'
curl -X PUT http://127.0.0.1:9200/test_file/sample/5 -d '{"id": "5","title": "Sun Flower In Morning - Episode 01"}'
curl -X PUT http://127.0.0.1:9200/test_file/sample/6 -d '{"id": "6","title": "Sun Flower In Morning - Episode 15"}'
curl -X PUT http://127.0.0.1:9200/test_file/sample/7 -d '{"id": "7","title": "Sun Flower In Morning - Episode 17"}'
curl -X PUT http://127.0.0.1:9200/test_file/sample/8 -d '{"id": "8","title": "Sun Flower In Morning - Episode 23"}'
现在,我尝试使用sql查询来实现这一目标:
result = (from d in context.FTDocuments
join f in context.FTDocFlags on d.ID equals f.DocID into fgrp
from x in fgrp.DefaultIfEmpty()
where d.LevelID == levelID && x.UserID == userID && d.Status.Equals(DocumentStatus.NEW)
select new Entities.Document
{
ArrivalDate = d.ArrivalDate.Value,
BundleReference = d.BundleRef,
CreatedDate = d.CreatedDate,
CustomerID = d.CustomerID,
DocType = d.DocType.Value,
GuidID = d.DocGuid,
ID = d.ID,
LastExportID = d.LastExpID,
LevelID = d.LevelID,
ProfileID = d.ProfileID,
ScanDate = d.ScanDate.Value,
ScanOperator = d.ScanOperator,
SenderEmail = d.SenderEmail,
Status = d.Status,
VerifyOperator = d.VerOperator,
FlagNo = x == null ? 0 : x.FlagNo,
FlagUserID = x == null ? 0 : x.UserID
}).ToList();
但是得到以下错误:
数据阅读器有多个字段。多个字段对EDM原语或枚举类型无效
是否可以使用上述复杂查询?
我使用EF 6.0。
答案 0 :(得分:5)
your query does not return a single string. Use like:
var test = context.Database.SqlQuery<Entities.Document>("...");
答案 1 :(得分:0)
试试这个
const string query = @"select *from FTDocument d left outer join FTDocFlag f on d.ID=f.DocID";
var test = context.Database.SqlQuery<Entity>(query).ToList();
答案 2 :(得分:0)
在我的回答中,我首先假设了 EntitityFramework(ObjectContext),但之后我又添加了 DbContext 的代码。
要查看下面的示例,您可以使用LinqPad并使用 EntitityFramework(ObjectContext)通过添加连接添加您的Entity Framework DLL。指定连接属性并关闭连接对话框。然后选择连接并运行示例:
void Main()
{
var context=this; // requires that you selected an EF ObjectContext connection
var q=context.ExecuteStoreQuery<FTDocument>(
"SELECT * FROM FTDocument WHERE ID = @p0", 1);
q.ToList().Dump();
}
它将接受所有类型的SQL查询,您可以使用@p0
,@p1
等参数,并在调用函数ExecuteStoreQuery
时简单地将它们以逗号分隔。结果将以List<FTDocument>
的形式返回。要将其转换为List<string>
,您需要指定要返回的数据库字段 - 或者在每行中创建以逗号分隔的字段值列表,例如:
q.Select(s=>s.ID+", "+s.GuidID+", "+s.DocType).ToList().Dump();
同样的例子,但这次使用的是 EntityFramework(DbContext):
使用 EntitityFramework(DbContext V4 / V5 / V6)通过添加连接添加实体框架DLL。指定连接属性(不要忘记指定AppConfig文件)并关闭连接对话框。然后选择连接并运行示例:
void Main()
{
var context=this; // requires that you selected an EF DBContext (V4/5/6) connection
var q=context.Database.SqlQuery<FTDocument>(
"SELECT * FROM FTDocument WHERE ID = @p0", 1);
q.ToList().Dump();
}
提示:在关闭连接对话框之前,请单击测试。如果您知道连接成功,它将在以后让您头疼。对于所有想要使用不同的EF项目和自己的数据库进行尝试的人,here is快速教程如何创建它。然后,只需将上面示例中的FTDocument
替换为您选择的另一个表(在SQL字符串中,当然在括号<...>
内)。