簡單的交集、並集、差集用法如下:
list<
string
> lista =
newlist
<
string
>()
;list<
string
> listb =
newlist
<
string
>()
;list<
string
> listresult =
newlist
<
string
>()
;listresult = lista.
distinct()
.tolist()
;//去重
listresult = lista.
except
(listb)
.tolist()
;//差集
listresult = lista.
union
(listb)
.tolist()
;//並集
listresult = lista.
intersect
(listb)
.tolist()
;//交集
若上面的例子不是list
型別,而是list
,則需要對***model進行處理。
步驟如下:
(1)先定義model。
public
class
itemmodel
public
string itemname
}
(2)定義如何model間如何比較。若不定義,比較的是兩個引用。
public
class
itemmodelcomparer
:iequalitycomparer
else
if(object.
referenceequals
(x,null
)|| object.
referenceequals
(y,null))
else
else
}return checkflag;
}//實現獲取雜湊值
public
intgethashcode
(itemmodel model)
}
(3)具體使用如下
list lista =
newlist
<
itemmodel
>()
;list listb =
newlist
<
itemmodel
>()
;list listresult =
newlist
<
itemmodel
>()
;listresult = lista.
distinct
(new
itemmodelcomparer()
).tolist()
;//去重
listresult = lista.
except
(listb,
newitemmodelcomparer()
).tolist()
;//差集
listresult = lista.
union
(listb,
newitemmodelcomparer()
).tolist()
;//並集
listresult = lista.
intersect
(listb,
newitemmodelcomparer()
).tolist()
;//交集
若出現兩個list,想用linq進行查詢。則可以使用如下的方式
int tempsatisfiedconditioncount = (from r1 in whitelist
join r2 in args.diaglist on r1.diagcode equals r2.diagcode
select r1).count();
多條件的join
//多條件的聯合查核
list whitediaglist =
(from r1 in diagcontrollist
where r1.controlrelation ==
1join r2 in args.
diaglist on new
equals new
select r1)
.tolist
<
diagcontrolmodel
>()
;
另外,join還可以用於左連線/右連線
var leftjoin = from emp in listofemployees
join dept in listofdepartment
on emp.deptid equals dept.id into joinedempdept
from dept in joinedempdept.defaultifempty()
select new
;
let是乙個在linq中定義區域性變數
1、可以有多個let子句
2、let後的變數無需宣告型別
參見下例:
var query = from a in list
let b = a.name.split('-')
let c=a.age
where b[0] =="zhangs" & c>21
select a;
常用的分組用法:
var linqtest = from r in db.am_recproscheme
orderby r.rpid descending
group r by r.rectype into n
select new
;
略微複雜點:
var datalist= (from r in druglabellist
group r by new
into q
let drugamount = q.sum(t => convert.toint32(t.chargeamount))
where drugamount >= 0
select new printdruglabelmodel
).tolist();
var linqtest = (from r in db.am_recproscheme
select r).sum(p => p.rpid);
var linqtest = (from r in db.am_recproscheme
where r.rpid > 10
orderby r.rpid descending //倒序
// orderby r.rpid, r.rpname descending //多條件的倒序(與sql語句中的相同)
// orderby r.rpid ascending //正序
select r);
//如果取最後乙個可以按倒敘排列再取值
var linqtest = (from r in db.am_recproscheme
select r).firstordefault();
var linqtest = (from r in db.am_recproscheme
where r.rpid > 10
orderby r.rpid descending
select r).skip(10).take(10); //取第11條到第20條資料
//可以使用list、array、string的contains()方法進行判斷
var linqtest = (from r in db.am_recproscheme
where r.sortstext.contains("張")
select r);
var linqtest = (from r in db.am_recproscheme
join w in db.am_test_result on r.rpid equals w.rsid
orderby r.rpid descending
select r);
//遍歷datatable,將其中的資料對應到clientstruct中:
listlist = (from x in dttable.asenumerable()
orderby x.field("") descending
where x.field("errortype") == "漏孔"
select new clientstruct
).tolist();
參考:的張龍豪部落格 C 常用用法總結
華電北風吹 2016 04 04 好久不刷題了,基礎都忘了還是總結一下常用的stl函式用法吧。無創新,純粹為了效率。一 vector用法 vector map n,vector n 二維初始化 struct edge bool compare edge e1,edge e2 vector v sor...
C 集合ArrayList常用用法
arraylist元素 集合練習 在陣列中放各種型別的資料,並且不確定數量。arraylist al new arraylist al.add 23 add 方法在集合中可以新增任何型別的資料 al.add 哈哈 arraylist 0 10 利用索引可以對資料重新賦值,但不能新增新的資料 arra...
CRUL常用用法
1 儲存http結果 curl o page.html 2設定 curl x 123.45.67.89 1080 o page.html 3 自動填寫使用者名稱密碼 curl u name passwd ftp ip port path file 4上傳檔案 curl t localfile u n...