为什么可以这样? .OrderBy(a => System.Guid.NewGuid())

stevenjin 2017-06-09 11:00:08
最近看到这样一种用法很是疑惑,
System.Guid.NewGuid()生成的是一个32位的字符串,
与OrderBy接收的不符啊。为什么可以这样呢

var album = db.Albumn
.OrderBy(a => System.Guid.NewGuid())
.First();
album.Price *= 0.5m;
...全文
588 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtee 2017-06-09
  • 打赏
  • 举报
回复
随机排序取第一个
stevenjin 2017-06-09
  • 打赏
  • 举报
回复
还是不明白,OrderBy总归得接收一个具体字段啊, 他那样子生成的一串随机字符串呢。
正怒月神 2017-06-09
  • 打赏
  • 举报
回复
OrderBy(a => System.Guid.NewGuid()) 意思就是随机排序
exception92 2017-06-09
  • 打赏
  • 举报
回复
a => System.Guid.NewGuid() =》匿名委托而已,只不过没有指定a的排序字段,还可以这样写呢 a => true。
xuzuning 2017-06-09
  • 打赏
  • 举报
回复
引用 3 楼 stevenjin 的回复:
还是不明白,OrderBy总归得接收一个具体字段啊, 他那样子生成的一串随机字符串呢。
不对!OrderBy 接受的是一个序列,并将对这个序列排序的结果关联到宿主
stevenjin 2017-06-09
  • 打赏
  • 举报
回复
老大说的好懂,可我去数据库试了下, order by 只接受表中存在的字段名,否则 报错 在这里为什么就能接收随机字符排序了呢,还是有疑惑呀
正怒月神 2017-06-09
  • 打赏
  • 举报
回复
引用 3 楼 stevenjin 的回复:
他那样子生成的一串随机字符串呢。
System.Guid.NewGuid()生成了字符串啊。 然后order会按照字符串的每个 char来排序。 System.Guid.NewGuid()生成出来每次都不一样。所以就确保了顺序肯定打乱
Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr. Wong Yahoo! Google Windows Live Send as Email Add to your CodeProject bookmarks Discuss this article 85 Print Article Database » Database » Other databasesLicence CPOL First Posted 19 Jan 2012 Views 24,219 Downloads 992 Bookmarked 74 times RaptorDB - The Key Value Store V2 By Mehdi Gholam | 8 Mar 2012 | Unedited contribution C#.NETDBABeginnerIntermediateAdvanceddatabase Even faster Key/Value store nosql embedded database engine utilizing the new MGIndex data structure with MurMur2 Hashing and WAH Bitmap indexes for duplicates. See Also More like this More by this author Article Browse Code Stats Revisions (8) Alternatives 4.95 (56 votes) 1 2 3 4 5 4.95/5 - 56 votes μ 4.95, σa 1.05 [?] Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions. Add your own alternative version Introduction What is RaptorDB? Features Why another data structure? The problem with a b+tree Requirements of a good index structure The MGIndex Page Splits Interesting side effects of MGIndex The road not taken / the road taken and doubled back! Performance Tests Comparing B+tree and MGIndex Really big data sets! Index parameter tuning Performance Tests - v2.3 Using the Code Differences to v1 Using RaptorDBString and RaptorDBGuid Global parameters RaptorDB interface Non-clean shutdowns Removing Keys Unit tests File Formats File Format : *.mgdat File Format : *.mgbmp File Format : *.mgidx File Format : *.mgbmr , *.mgrec History Download RaptorDB_v2.0.zip - 38.7 KB Download RaptorDB_v2.1.zip - 39 KB Download RaptorDB_v2.2.zip - 39 KB Download RaptorDB_v2.3.zip - 39.6 KB D

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧