乙個從委託應用到LINQ演變的示例

2021-05-23 13:10:05 字數 4635 閱讀 9105

一,乙個賽車手的類表述了賽車手的姓名,國籍,獲獎情況等資訊

[serializable]

public

class racer : icomparable, iformattable

public

string lastname

public

int wins

public

string country

public

int starts

public

string cars

public

int years

public

override

string tostring()

", firstname, lastname);

}public

string tostring(string format)

public

string tostring(string format, iformatprovider formatprovider)

, ; starts: , wins: ",

firstname, lastname, country, starts, wins);

default:

throw

new formatexception(string.format(

"format not supported", format));}}

public

int compareto(racer other)

}

二,生成賽車手list的類,提供乙個方法生成list

public

class formula1

, cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

racers.add(new racer() , cars = new

string });

return racers;

}}

三,示例**

static

class program

", r);

r.putinfo(datetime.now);

}//使用匿名委委託

racers = new list(formula1.getchampions());

brazilracers = racers.findall(

delegate(racer r)

);brazilracers.sort(

delegate(racer r1, racer r2)

);foreach (racer r in brazilracers)

", r);

r.putinfo(datetime.now);

}//使用linq類提供的擴充套件方法

racers = new list(formula1.getchampions());

brazilracers.where(

delegate(racer r)

);brazilracers.orderbydescending(

delegate(racer r)

);brazilracers.select(

delegate(racer r)

);foreach (racer r in brazilracers)

", r);

r.putinfo(datetime.now);

}//運用lamda表括達式

racers = new list(formula1.getchampions());

racers.where(r => r.country == "brazil");

racers.orderbydescending(r => r.wins);

racers.select(r => r);

foreach (racer r in brazilracers)

", r);

r.putinfo(datetime.now);

}//最終演變為linq查詢

racers = new list(formula1.getchampions());

var query = from r in racers

where r.country == "brazil"

orderby r.wins descending

select r;

foreach (racer r in query)

", r);

r.putinfo(datetime.now);

}console.read();

}///

/// 擴充套件方法例項

/// 呼叫擴充套件方法只是呼叫靜態方法的一種新語法

///

///

public

static

void putinfo(this racer r, datetime dt)

", dt.tolongtimestring());

}///

/// 用於查詢

///

public

static predicate isbrazilhandle = isbrazil;

public

static

bool isbrazil(racer r)

else

}///

/// 用於排序

///

public

static comparison sortracershandle = new comparison(sortracers);

public

static

int sortracers(racer r1, racer r2)

}

說明:該示例參照c#高階程式設計中關於linq的講解

測試從應用到DB的準確的網路延遲

最近幾大雲廠商把底層的基礎設施包裝起來後,通過ping等很難知道真正的網路延遲了,因為通過ping還沒到db都返回了,如何知道從應用到db的真正網路延遲呢?用我的指令碼 bin bash m 1db port 1 db port 2 db user 3 db pass 4 echo res.txt ...

關於a標籤不能應用到整個li標籤的一些小問題

今天在學習bootstrap的時候,剛學到列表排版,心中一動就想做乙個簡單的導航欄出來,就當複習複習。但我寫完之後發現滑鼠只有移動到文字的時候才能變成乙個手,在 li的區域毫無反應,我就想著改變一下a標籤的高度和寬度,但發現並沒有用,最後查了一下,發現a標籤是行內元素 inline 行內元素 inl...

較為複雜的乙個linq語句

作為乙個初學者,這個句子花了我好大的力氣才寫出來,寫出來是次要的,重要的是又學到了一點新的用法。var query from gb in objectcontext.goodsclose back where gb.s sf comid gb.s month month join gw in 類似子...