zoukankan      html  css  js  c++  java
  • linq to entity 分页方法不支持LastOrDefault等操作的处理办法

    最近一直使用entityframework,因为项目需要涉及到需要查询最后一条,我看着里面扩展了LastOrDefault这个方法,但是使用的时候一直报错

    “LINQ to Entities 不识别方法“TravelWeb.Dao.XT_Card LastOrDefault[XT_Card](System.Linq.IQueryable`1[TravelWeb.Dao.XT_Card])”,因此该方法无法转换为存储表达式。”这样搞得我很郁闷,后来查了一下资料结果在MSDN上面查到了

    支持和不支持的 LINQ 方法 (LINQ to Entities)

    投影和限制方法

    LINQ to Entities 查询中支持大多数 LINQ 投影和限制方法,除了那些接受位置参数的方法。 下表列出了支持的和不支持的投影和限制方法。

     

    分页方法

    有些 LINQ 分页方法在 LINQ to Entities 查询中不受支持。 有关更多信息,请参见 LINQ to Entities 查询中的标准查询运算符 下表列出了支持的和不支持的分页方法。

    方法 支持 Visual Basic 函数签名 C# 方法签名

    ElementAt

    不支持

    Function ElementAt(Of TSource) ( _
    source As IQueryable(Of TSource), _
    index As Integer _
    ) As TSource
    
    TSource ElementAt<TSource>(
    this IQueryable<TSource> source,
    int index
    )
    

    ElementAtOrDefault

    不支持

    Function ElementAtOrDefault(Of TSource) ( _
    source As IQueryable(Of TSource), _
    index As Integer _
    ) As TSource
    
    TSource ElementAtOrDefault<TSource>(
    this IQueryable<TSource> source,
    int index
    )
    

    First

    支持

    Function First(Of TSource) ( _
    source As IQueryable(Of TSource) _
    ) As TSource
    
    TSource First<TSource>(
    this IQueryable<TSource> source
    )
    

    First

    支持

    Function First(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As TSource
    
    TSource First<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    FirstOrDefault

    支持

    Function FirstOrDefault(Of TSource) ( _
    source As IQueryable(Of TSource) _
    ) As TSource
    
    TSource FirstOrDefault<TSource>(
    this IQueryable<TSource> source
    )
    

    FirstOrDefault

    支持

    Function FirstOrDefault(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As TSource
    
    TSource FirstOrDefault<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    Last

    不支持

    Function Last(Of TSource) ( _
    source As IQueryable(Of TSource) _
    ) As TSource
    
    TSource Last<TSource>(
    this IQueryable<TSource> source
    )
    

    Last

    不支持

    Function Last(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As TSource
    
    TSource Last<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    LastOrDefault

    不支持

    Function LastOrDefault(Of TSource) ( _
    source As IQueryable(Of TSource) _
    ) As TSource
    
    TSource LastOrDefault<TSource>(
    this IQueryable<TSource> source
    )
    

    LastOrDefault

    不支持

    Function LastOrDefault(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As TSource
    
    TSource LastOrDefault<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    Single

    支持

    Function Single(Of TSource) ( _
    source As IQueryable(Of TSource) _
    ) As TSource
    
    TSource Single<TSource>(
    this IQueryable<TSource> source
    )
    

    Single

    支持

    Function Single(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As TSource
    
    TSource Single<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    SingleOrDefault

    支持

    Function SingleOrDefault(Of TSource) ( _
    source As IQueryable(Of TSource) _
    ) As TSource
    
    TSource SingleOrDefault<TSource>(
    this IQueryable<TSource> source
    )
    

    SingleOrDefault

    支持

    Function SingleOrDefault(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As TSource
    
    TSource SingleOrDefault<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    Skip

    支持

    Function Skip(Of TSource) ( _
    source As IQueryable(Of TSource), _
    count As Integer _
    ) As IQueryable(Of TSource)
    
    IQueryable<TSource> Skip<TSource>(
    this IQueryable<TSource> source,
    int count
    )
    

    SkipWhile

    不支持

    Function SkipWhile(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As IQueryable(Of TSource)
    
    IQueryable<TSource> SkipWhile<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    SkipWhile

    不支持

    Function SkipWhile(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Integer, Boolean)) _
    ) As IQueryable(Of TSource)
    
    IQueryable<TSource> SkipWhile<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, int, bool>> predicate
    )
    

    Take

    支持

    Function Take(Of TSource) ( _
    source As IQueryable(Of TSource), _
    count As Integer _
    ) As IQueryable(Of TSource)
    
    IQueryable<TSource> Take<TSource>(
    this IQueryable<TSource> source,
    int count
    )
    

    TakeWhile

    不支持

    Function TakeWhile(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
    ) As IQueryable(Of TSource)
    
    IQueryable<TSource> TakeWhile<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
    )
    

    TakeWhile

    不支持

    Function TakeWhile(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Integer, Boolean)) _
    ) As IQueryable(Of TSource)
    

     

    

    发现这么多不支持的。我现在暂时用到lastordefault  处理办法就是用orderby 一个是顺序排列一个是倒序排列,不过切记where 条件要写在前面

    示例

    CardStartNum =edm.XT_Card.Where(c => c.CardRuleId == s.Id).OrderBy(c => c.CardNum).FirstOrDefault(),
     CardEndNum = edm.XT_Card.Where(c => c.CardRuleId == s.Id).OrderByDescending(c => c.CardNum).FirstOrDefault()

    搞定!

  • 相关阅读:
    Protocol Buffer使用
    uImage、zImage、bzImage、vlinzx区别
    nginx学习之一
    Android屏幕底部弹出DialogFragment(3)
    C++11 | 正则表达式(4)
    Android绘图之渐隐动画
    Android动态Java代码调整window大小
    Spring Boot交流平台
    Java日志实战及解析
    Android WindowManager悬浮窗:不需要申请权限实现悬浮
  • 原文地址:https://www.cnblogs.com/hack1506/p/2097767.html
Copyright © 2011-2022 走看看