SqlFunctionEx.cs 819 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Entity.Core.Objects.DataClasses;
  6. namespace Bowin.Common.Linq.Entity
  7. {
  8. public static class SqlFunctionEx
  9. {
  10. /// <summary>
  11. /// 在linq to entity中使用SqlServer.NEWID函数
  12. /// </summary>
  13. [EdmFunction("SqlServer", "NEWID")]
  14. public static Guid NewId()
  15. {
  16. return Guid.NewGuid();
  17. }
  18. /// <summary>
  19. /// 随机排序扩展方法
  20. /// </summary>
  21. /// <typeparam name="T"></typeparam>
  22. /// <param name="source"></param>
  23. /// <returns></returns>
  24. public static IQueryable<T> OrderByNewId<T>(this IQueryable<T> source)
  25. {
  26. return source.OrderBy(d => NewId());
  27. }
  28. }
  29. }