代码:
using (var panoContext = new UnitOfWorkWithIPCPano()) //分布查询解决上下文不一致的缺陷
{
using (var accountContext = new UnitOfWorkWithIPCUser())
{
Expression<Func<Pano, bool>> filter = o => true;
if (!string.IsNullOrEmpty(keywords)) //按关键词过滤查询
{
keywords = keywords.Trim();
filter = o => (null == o.Name ? false : o.Name.Contains(keywords)
|| null == o.Address ? false : o.Address.Contains(keywords)
|| null == o.City ? false : o.City.Contains(keywords)
|| null == o.Country ? false : o.Country.Contains(keywords));
//filter = o => (o.Name.Contains(keywords) || o.Address.Contains(keywords));
}
if (deleteFlag)
{
filter = filter.And(o => o.DeleteFlag == false);
} //个人全景列表 if (accountId != 0)
{
filter = filter.And(o => o.AccountId == accountId);
}
List<PanoDetail> panoDetailList = new List<PanoDetail>();
try
{
var panoQuery = panoContext.PanoRepository.GetAll().Where(filter.Compile());
然后虽然keyword关键字有值并且数据库里也有匹配的字段值 但就是无法查询出来,生成的sql语句是:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[SN] AS [SN],
[Extent1].[AccountId] AS [AccountId],
[Extent1].[Name] AS [Name],
[Extent1].[Type] AS [Type],
[Extent1].[Detail] AS [Detail],
[Extent1].[IsRecommend] AS [IsRecommend],
[Extent1].[IsPublic] AS [IsPublic],
[Extent1].[Country] AS [Country],
[Extent1].[Direction] AS [Direction],
[Extent1].[City] AS [City],
[Extent1].[Address] AS [Address],
[Extent1].[GisX] AS [GisX],
[Extent1].[GisY] AS [GisY],
[Extent1].[Platform] AS [Platform],
[Extent1].[P] AS [P],
[Extent1].[T] AS [T],
[Extent1].[F] AS [F],
[Extent1].[Category] AS [Category],
[Extent1].[BlockCount] AS [BlockCount],
[Extent1].[SharePwd] AS [SharePwd],
[Extent1].[LocalId] AS [LocalId],
[Extent1].[ClientIP] AS [ClientIP],
[Extent1].[IsNew] AS [IsNew],
[Extent1].[Storage] AS [Storage],
[Extent1].[Path] AS [Path],
[Extent1].[AudioPath] AS [AudioPath],
[Extent1].[FisheyeCount] AS [FisheyeCount],
[Extent1].[ThumbnailUrl] AS [ThumbnailUrl],
[Extent1].[DeleteFlag] AS [DeleteFlag],
[Extent1].[ShootTime] AS [ShootTime],
[Extent1].[CreateTime] AS [CreateTime],
[Extent1].[UpdateTime] AS [UpdateTime]
FROM [dbo].[Pano] AS [Extent1]
你这··还是别了。不然就算做出来也没法上线的。。
大神,求教 为什么呢 请指教?
@梦里追逐: 模糊匹配会造成全表扫描.而且效率非常非常低.
而且你还用or.这种作死行为真的是````
真有这种需求的话,还是看看全文索引吧.高端点就去弄下路身.net
@吴瑞祥: 全文索引指的是数据库的全文索引吧? 路神.net 指的是Lucene.net ? 我查了一下 lucene.net好像是以文本的形式简历索引库,然后从索引库里读取数据,但是我想做到的 读取整个表中模糊查询的所有结果的所有字段 ?
@梦里追逐: 你要是直接这样模糊匹配,而且还是这么多个字段的,还是逻辑或的.如果真照你这样实现,数据上10W就不行了.你这个情况可以加一个字段,把要搜索的字段的值都拼接起来,然后对这个字段做全文索引.
这样搜索起来效率会高很多而且省去了过程
using(var db = new EFContent())
{
//根据姓名、课程模糊查询
var poco = db.表名.AsQueryable();
if(name!=null||name!=""){
poco = poco.where(p=>p.Name.Contains(name));
}
if(cuse!=null||cuse!=""){
poco = poco.where(p=>p.Cuse.Contains(cuse));
}
if(poco!=null||poco.Count()!=0){
//获取数据后的操作
}
}