【问题标题】:ASP.NET MVC 3 Linq uppercase or lowercase database searchASP.NET MVC 3 Linq 大写或小写数据库搜索
【发布时间】:2012-07-02 09:07:52
【问题描述】:

我需要立即帮助。 ): 我不懂英语。

ASP.NET MVC 3 Linq 大写或小写包含搜索

例子:

string metin="baris";

var IcerikAra = (from icerik in Context.dbDokumanEditor
join kategori in Context.dbDokumanKategori on 
icerik.KategoriID equals kategori.KategoriID
where icerik.Icerik.toLower().Contains(metin)
select new { 
KategoriID=kategori.
KategoriAd=kategori.KategoriAd
}).ToList();

例外: 堆栈跟踪:

at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommandentityCommand, CommandBehavior behavior)
at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Plus.Areas.DokumanEditor.Controllers.DokumanController.DokumanIcerikAramaBaslat(String metin)

错误消息:执行命令定义时发生错误。有关详细信息,请参阅内部异常。

谢谢..

【问题讨论】:

  • See the inner exception for details. 你这样做了吗?如果,是的,你看到了什么?如果没有,请继续。
  • 您的选择块错误。一定是这样的select new { KategoriID=kategori.KategoriID, KategoriAd=kategori.KategoriAd }。选择 kısmında sorun var, ayrıca iç hatada ne yazıyor onu da yazarsan sornunun hakkında daha doğru cevaplar alabilirsin

标签: c# linq entity-framework


【解决方案1】:

在 where 子句中使用 String.Equals

where icerik.Icerik.Equals(metin,StringComparison.CurrentCultureIgnoreCase)

【讨论】:

    【解决方案2】:

    我解决如下:

    database datatype text -> old
    database datatype nvarchar(MAX) -> new
    
    var IcerikAra = (from icerik in Context.dbDokumanEditor
                     join kategori in Context.dbDokumanKategori on icerik.KategoriID equals kategori.KategoriID
    where icerik.Icerik.ToLower().Contains(metin)
    select new { 
         KategoriID=kategori.KategoriID,
         KategoriAd=kategori.KategoriAd
    }).ToList();
    

    【讨论】:

    • 问题不清楚,所以答案也不清楚。另外,请注意,这种ToLower 转换根本没有必要,因为数据库排序规则决定了所有字符串比较。
    • 我将数据类型从数据库更改为 nvarchar (max)。如果 metin 变量是小写的,可以通过小写在 Icerik 中进行搜索。
    • 如果数据库排序规则不区分大小写(可能是这样),那么您确实不需要需要这个 ToLower。
    猜你喜欢
    • 2017-02-21
    • 1970-01-01
    • 2023-03-10
    • 2021-07-04
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多