Linq 入门系列 [OfType,ToArray,ToList,ToDictionary]
生活随笔
收集整理的這篇文章主要介紹了
Linq 入门系列 [OfType,ToArray,ToList,ToDictionary]
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
先說點(diǎn)理論1.OfType?:
?????即接受基于IEnumerable<T>?接口的信息源,也接受那些在?.NET?Framework?1.0?中出現(xiàn)的非參數(shù)化的?IEnumerable?接口(non-parameterized?IEnumerable?interface)。OfType?操作符允許用戶在標(biāo)準(zhǔn)的?.NET?collections?類(classic?.NET?collections)上應(yīng)用標(biāo)準(zhǔn)查詢操作符,就像下面的程序:
代碼:
IEnumerable?classic?=?new?OlderCollectionType();
IEnumerable<object>?modern?=?classic.OfType<object>();
在這個例子中,變量?modern?產(chǎn)生了與變量?classic?一樣的順序的數(shù)據(jù)列表(same?sequence?of?values),但是不同的是它的類型兼容最新的?IEnumerable<T>?代碼(modern?IEnumerable<T>?code),包括標(biāo)準(zhǔn)查詢操作符。
OfType?操作符對新的信息源也是有用的,因?yàn)樗试S從基于類型的信息源(source?based?on?type)中過濾出數(shù)據(jù)(filtering?values?from)。當(dāng)要制作新的順序的時候(producing?the?new?sequence),OfType?簡單的忽略(omits)原有序列的成員(members?of?the?original?sequence)就可以了,這是與類型實(shí)參(type?argument)不相符的。分析下面的程序,目標(biāo)是將?string?字符串?dāng)?shù)據(jù)從一個有不同種類數(shù)據(jù)的數(shù)組(heterogeneous?array)中分解出來:
代碼:
object[]?vals?=?{?1,?"Hello",?true,?"World",?9.1?};
IEnumerable<string>?justStrings?=?vals.OfType<string>();
當(dāng)在?foreach?語句中列舉(enumerate)變量?justStrings?中的數(shù)據(jù)時,將得到兩個依次為“Hello”和“World”的?string?字符串序列(a?sequence?of?two?strings?)。
2.ToList,ToArray:
??對應(yīng)用程序想緩存查詢賦值的結(jié)果,ToList?和?ToArray?這兩個操作符提供用來強(qiáng)制查詢的直接賦值(force?the?immediate?evaluation),以返回一個含有查詢賦值的結(jié)果的?List<T>?或者Array?數(shù)組。
為了解延遲查詢賦值是怎么工作的,請考察下面一段簡單地查詢一個數(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);
查詢在每次變量?ayes?迭代結(jié)束(iterated?over)時賦值。為了顯示需要對結(jié)果做一份緩存的copy,我們可以簡單給這個查詢附加上一個?ToList?或?一個?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)制查詢的賦值,就像執(zhí)行一個標(biāo)準(zhǔn)查詢操作符(如?First,?ElementAt,?Sum,?Average,?All,?等)返回一個單獨(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]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET DEMO 12 : Ch
- 下一篇: ASP.NET 程序中常用的三十三种代码