• 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()

    搞定!

  • 相关阅读:
    ASP脚本获取服务器全部参数列表说明
    HTML基础教程
    HTML5代码大全
    CSS 属性大全
    Web前端单词大全
    css常用代码大全
    曾国藩:诚敬静谨恒!
    鼠标经过显示菜单
    月入3000+项目
    右侧菜单显示隐藏
  • 原文地址:https://www.cnblogs.com/hack1506/p/2097767.html
Copyright © 2020-2023  润新知