當(dāng)前位置:
首頁(yè) >
Linq 入门系列 [OfType,ToArray,ToList,ToDictionary]
發(fā)布時(shí)間:2025/7/14
35
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Linq 入门系列 [OfType,ToArray,ToList,ToDictionary]
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
先說(shuō)點(diǎn)理論1.OfType?:
?????即接受基于IEnumerable<T>?接口的信息源,也接受那些在?.NET?Framework?1.0?中出現(xiàn)的非參數(shù)化的?IEnumerable?接口(non-parameterized?IEnumerable?interface)。OfType?操作符允許用戶(hù)在標(biāo)準(zhǔn)的?.NET?collections?類(lèi)(classic?.NET?collections)上應(yīng)用標(biāo)準(zhǔn)查詢(xún)操作符,就像下面的程序:
代碼:
IEnumerable?classic?=?new?OlderCollectionType();
IEnumerable<object>?modern?=?classic.OfType<object>();
在這個(gè)例子中,變量?modern?產(chǎn)生了與變量?classic?一樣的順序的數(shù)據(jù)列表(same?sequence?of?values),但是不同的是它的類(lèi)型兼容最新的?IEnumerable<T>?代碼(modern?IEnumerable<T>?code),包括標(biāo)準(zhǔn)查詢(xún)操作符。
OfType?操作符對(duì)新的信息源也是有用的,因?yàn)樗试S從基于類(lèi)型的信息源(source?based?on?type)中過(guò)濾出數(shù)據(jù)(filtering?values?from)。當(dāng)要制作新的順序的時(shí)候(producing?the?new?sequence),OfType?簡(jiǎn)單的忽略(omits)原有序列的成員(members?of?the?original?sequence)就可以了,這是與類(lèi)型實(shí)參(type?argument)不相符的。分析下面的程序,目標(biāo)是將?string?字符串?dāng)?shù)據(jù)從一個(gè)有不同種類(lèi)數(shù)據(jù)的數(shù)組(heterogeneous?array)中分解出來(lái):
代碼:
object[]?vals?=?{?1,?"Hello",?true,?"World",?9.1?};
IEnumerable<string>?justStrings?=?vals.OfType<string>();
當(dāng)在?foreach?語(yǔ)句中列舉(enumerate)變量?justStrings?中的數(shù)據(jù)時(shí),將得到兩個(gè)依次為“Hello”和“World”的?string?字符串序列(a?sequence?of?two?strings?)。
2.ToList,ToArray:
??對(duì)應(yīng)用程序想緩存查詢(xún)賦值的結(jié)果,ToList?和?ToArray?這兩個(gè)操作符提供用來(lái)強(qiáng)制查詢(xún)的直接賦值(force?the?immediate?evaluation),以返回一個(gè)含有查詢(xún)賦值的結(jié)果的?List<T>?或者Array?數(shù)組。
為了解延遲查詢(xún)賦值是怎么工作的,請(qǐng)考察下面一段簡(jiǎn)單地查詢(xún)一個(gè)數(shù)組的程序:
代碼:
//?declare?a?variable?containing?some?strings
string[]?names?=?{?"Allen",?"Arthur",?"Bennett"?};
//?declare?a?variable?that?represents?a?query
IEnumerable<string>?ayes?=?names.Where(s?=>?s[0]?==?'A');
//?evaluate?the?query
foreach?(string?item?in?ayes)
??Console.WriteLine(item);
//?modify?the?original?information?source
names[0]?=?"Bob";
//?evaluate?the?query?again,?this?time?no?"Allen"
foreach?(string?item?in?ayes)
????Console.WriteLine(item);
查詢(xún)?cè)诿看巫兞?ayes?迭代結(jié)束(iterated?over)時(shí)賦值。為了顯示需要對(duì)結(jié)果做一份緩存的copy,我們可以簡(jiǎn)單給這個(gè)查詢(xún)附加上一個(gè)?ToList?或?一個(gè)?ToArray?操作符,如下所示:
代碼:
//?declare?a?variable?containing?some?strings
string[]?names?=?{?"Allen",?"Arthur",?"Bennett"?};
//?declare?a?variable?that?represents?the?result
//?of?an?immediate?query?evaluation
string[]?ayes?=?names.Where(s?=>?s[0]?==?'A').ToArray();
//?iterate?over?the?cached?query?results
foreach?(string?item?in?ayes)
????Console.WriteLine(item);
//?modifying?the?original?source?has?no?effect?on?ayes
names[0]?=?"Bob";
//?iterate?over?result?again,?which?still?contains?"Allen"
foreach?(string?item?in?ayes)
????Console.WriteLine(item);
ToList?和?ToArray?都強(qiáng)制查詢(xún)的賦值,就像執(zhí)行一個(gè)標(biāo)準(zhǔn)查詢(xún)操作符(如?First,?ElementAt,?Sum,?Average,?All,?等)返回一個(gè)單獨(dú)的值(singleton?values)一樣。
ToArray:
public?void?ToArray()?{
????double[]?doubles?=?{?1.7,?2.3,?1.9,?4.1,?2.9?};
????var?sortedDoubles?=
????????from?d?in?doubles
????????orderby?d?descending
????????select?d;
????var?doublesArray?=?sortedDoubles.ToArray();
????Console.WriteLine("Every?other?double?from?highest?to?lowest:");
????for?(int?d?=?0;?d?<?doublesArray.Length;?d?+=?2)?{
????????Console.WriteLine(doublesArray[d]);
????}?
}
ToList:
public?void?ToList()?{
????string[]?words?=?{?"cherry",?"apple",?"blueberry"?};
????var?sortedWords?=
????????from?w?in?words
????????orderby?w
????????select?w;
????var?wordList?=?sortedWords.ToList();
????Console.WriteLine("The?sorted?word?list:");
????foreach?(var?w?in?wordList)?{
????????Console.WriteLine(w);
????}
}
ToDictionary:
public?void?ToDictionary()?{
????var?scoreRecords?=?new?[]?{?new?{Name?=?"Alice",?Score?=?50},
????????????????????????????????new?{Name?=?"Bob"?,?Score?=?40},
????????????????????????????????new?{Name?=?"Cathy",?Score?=?45}
??????????????????????????????};
????var?scoreRecordsDict?=?scoreRecords.ToDictionary(sr?=>?sr.Name);
????Console.WriteLine("Bob's?score:?{0}",?scoreRecordsDict["Bob"]);
}
OfType:
public?void?OfType()?{
????object[]?numbers?=?{?null,?1.0,?"two",?3,?4.0f,?5,?"six",?7.0?};
????var?doubles?=?numbers.OfType<double>();
????Console.WriteLine("Numbers?stored?as?doubles:");
????foreach?(var?d?in?doubles)?{
????????Console.WriteLine(d);
????}
}
ToArray:Result
Every?other?double?from?highest?to?lowest:
4.1
2.3
1.7
ToList:Result
The?sorted?word?list:
apple
blueberry
cherry
ToDictionary:Result
Bob's?score:?{Name=Bob,?Score=40}
OfType:Result
Numbers?stored?as?doubles:
1
7
轉(zhuǎn)載于:https://www.cnblogs.com/RuiLei/archive/2007/07/19/823871.html
總結(jié)
以上是生活随笔為你收集整理的Linq 入门系列 [OfType,ToArray,ToList,ToDictionary]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET DEMO 12 : Ch
- 下一篇: GWT笔记(Google Web Too