string _UserID = string .Empty;
_UserID = "E351D301-F64B-412C-B9EF-573F41235AF2" ;
string _UserName = string .Empty;
_UserName = "admin" ;
string _employyName = string .Empty;
_employyName = "测试1" ;
using ( var xj = new XJGasBottles_testDataContext())
{
//Linq写法
var usersLinq = from us in xj.Users
where ( string .IsNullOrEmpty(_UserID) || us.UserID.ToString() == _UserID)
&& ( string .IsNullOrEmpty(_UserName) || us.UserName == _UserName)
|| (us.EmpName == _employyName)
//where string.IsNullOrEmpty(_UserID) || us.UserID.ToString()==_UserID
//where string.IsNullOrEmpty(_UserName) || us.UserName==_UserName
select us;
foreach ( var item in usersLinq)
{
Console.WriteLine( "Linq:" );
Console.WriteLine(item.UserID + "_" + item.UserName);
}
//Lamda写法
var usersLamda = xj.Users.Where(s => ( string .IsNullOrEmpty(_UserID) || s.UserID.ToString() == _UserID) &&
( string .IsNullOrEmpty(_UserName) || s.UserName == _UserName) ||
(s.EmpName==_employyName)
)
.Select(s => s);
foreach ( var item in usersLamda)
{
Console.WriteLine( "Lamda:" );
Console.WriteLine(item.UserID + "_" + item.UserName);
}
}
|