第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我數(shù)字與列表-使用什么?它們是如何工作的?

我數(shù)字與列表-使用什么?它們是如何工作的?

守著一只汪 2019-06-29 14:42:49
我數(shù)字與列表-使用什么?它們是如何工作的?我對枚舉器的工作方式和LINQ有一些疑問??紤]這兩個簡單的選擇:List<Animal> sel = (from animal in Animals                      join race in Species                     on animal.SpeciesKey equals race.SpeciesKey                     select animal).Distinct().ToList();或IEnumerable<Animal> sel = (from animal in Animals                             join race in Species                            on animal.SpeciesKey equals race.SpeciesKey                            select animal).Distinct();我更改了原始對象的名稱,使其看起來像一個更通用的示例。查詢本身并不那么重要。我想問的是:foreach (Animal animal in sel) { /*do stuff*/ }我注意到如果我用IEnumerable,當(dāng)我調(diào)試和檢查“sel”(在這種情況下是IEnDigable)時,它有一些有趣的成員:“innerKeySelector”、“innerKeySelector”和“outerKeySelector”,最后兩個成員似乎是委托?!皟?nèi)部”成員沒有“動物”實例,而是“物種”實例,這對我來說很奇怪?!巴獠俊背蓡T確實包含“動物”實例。我想是由兩位代表來決定哪些是進去的,哪些是從里面出來的?我注意到,如果使用“DISTISTION”,“Inside”包含6項(這是不正確的,因為只有2項是不同的),但是“外部”確實包含正確的值。同樣,可能是委托方法決定了這一點,但這比我對IEnDigable的了解要多一點。最重要的是,這兩種選擇中哪一種表現(xiàn)最好?通過.ToList()?或者直接使用枚舉器?如果可以的話,也請解釋一下或拋出一些鏈接來解釋IEnDigable的這種用法。
查看完整描述

2 回答

?
POPMUISE

TA貢獻1765條經(jīng)驗 獲得超5個贊

這里有一篇很好的文章:克勞迪奧·伯納斯科尼的TechBlog:何時使用IEnumber,IC,IList和List


查看完整回答
反對 回復(fù) 2019-06-29
?
不負相思意

TA貢獻1777條經(jīng)驗 獲得超10個贊

實現(xiàn)的類IEnumerable允許您使用foreach語法。

基本上,它有一個方法來獲取集合中的下一個項。它不需要整個集合在內(nèi)存中,也不知道其中有多少項,foreach一直拿到下一件直到用完為止。

在某些情況下,這可能非常有用,例如,在大規(guī)模數(shù)據(jù)庫表中,在開始處理行之前,您不希望將整個事件復(fù)制到內(nèi)存中。

現(xiàn)在List實施器IEnumerable,但表示內(nèi)存中的整個集合。如果你有IEnumerable然后你打電話.ToList()創(chuàng)建一個新列表,其中包含內(nèi)存中枚舉的內(nèi)容。

Linq表達式返回枚舉,默認情況下,當(dāng)您使用foreach..阿IEnumerable循環(huán)時執(zhí)行Linq語句。foreach,但您可以強制它更快地使用.ToList().

我的意思是:

var things = 
    from item in BigDatabaseCall()
    where ....
    select item;// this will iterate through the entire linq statement:int count = things.Count();// this will stop after iterating the first one, but will execute the linq againbool hasAnyRecs = things.Any();// this will execute the linq statement *again*foreach( var thing in things ) ...// this will copy the results to a list in memoryvar list = things.ToList()// this won't iterate through again, the list knows how many items are in itint count2 = list.Count();// this won't execute the linq statement - we have it copied to the listforeach( var thing in list ) ...


查看完整回答
反對 回復(fù) 2019-06-29
  • 2 回答
  • 0 關(guān)注
  • 452 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號