C 泛型分組和Linq分組的異同

2021-09-07 11:07:20 字數 3025 閱讀 5336

沒什麼好說的,因為用的到,所以作個記錄,

**如下:

using

system;

using

system.collections.generic;

using

system.linq;

using

system.text;

using

system.threading.tasks;

namespace

consoleme

));

foreach (var item in

ls)

console.writeline(""

); console.writeline(

"liqn分組如下:");

var ls2 = from ps in

persons1

group ps by ps.***

into g

select

new ;

foreach (var item in

ls2)

console.read();}}

public

class

person

public

int age

public

string ***

public

int money

public person(string name, string ***, int age, int

money)}}

執行截圖:

後續...

敬請期待...

如果是多列/多個屬性參與分組應當如何呢?

**如下:

namespace

test2

).select(g => (new ));

foreach (var item in

ls)

console.writeline(""

); console.writeline(

"liqn多屬性/多列分組如下:");

var ls2 = from ps in

persons1

group ps by

new into g

select

new ;

foreach (var item in

ls2)

console.read();}}

public

class

person

public

int age

public

string ***

public

int ***id

public

int money

public person(string name, string ***, int age, int money, int

***id)

}public

class

persongroup

public

int ***id

public listpersonls

}public

class

newperson

public

int age

public

int money }}

未完,持續...

敬請期待...

也可通過nuget獲取

namespace

consoleme

).select(g => (new ));

foreach (var item in

ls)

console.writeline(""

); console.writeline(

"liqn多屬性/多列分組如下:");

var ls2 = from ps in

persons1

select

new ;

var kar = ls2.groupby(g => new ).select(s => new );

//使用automap 將 kar 對映到對應的類

foreach (var item in

alllist)

}var data =kar.tolist();

console.read();}}

public

class

person

public

int age

public

string ***

public

int ***id

public

int money

public person(string name, string ***, int age, int money, int

***id)

}public

class

persongroup

public

int ***id

public listpersonlist

}public

class

newperson

public

int age

public

int money }}

以上**適用於:一對多的類對映

@陳臥龍的部落格

LINQ和泛型型別 C

linq 查詢基於泛型型別,在 net framework 的 2.0 版中引入了泛型型別。您無需深入了解泛型即可開始編寫查詢。但是,您可能需要了解兩個基本概念 當您建立泛型集合類 如 list of t 的例項時,您將 t 替換為列表將包含的物件的型別。例如,字串列表表示為 list custom...

C 實現泛型List分組輸出元素的方法

背景 在輸出列表時,往往需要按照某一字段進行分組,比如在輸出城市列表時,按照首字母進行分組,輸出學生列表時,按照年級進行分組,然後再對分組的結果按照其他的字段進行排序。如存在以下stu學生類,如下 public class stu public string name public int age ...

GroupBy分組的運用和linq左連線

var conhistorylist conhistorydata.groupby g g.personid 就是conhistorydata是乙個iqueryable型別 var conhistorylist conhistorydata.groupby g g.personid select g...