linq 基本用法
新建一个类
public class FolderBE
{
public string FolderOne
{ get; set; }
public string FolderTwo
{ get; set; }
public string FolderThree
{ get; set; }
public string FolderFour
{ get; set; }
public string FolderFive
{ get; set; }
}
然后
List<FolderBE> listFolder = new List<FolderBE>(); FolderBE folder = new FolderBE();
folder.FolderOne = "one";
folder.FolderTwo = "two";
folder.FolderThree = "three";
listFolder.Add(folder);
写一个搜索方法:
private bool CheckFolderExist(FolderBE folder, int item)
{
List<FolderBE> HavelistFolder = new List<FolderBE>();
if (item == 1)
{
HavelistFolder = (from entity in listFolder
where entity.FolderOne == folder.FolderOne &&
string.IsNullOrEmpty(entity.FolderTwo)
select entity).ToList();
}
else if (item == 2)
{
HavelistFolder = (from entity in listFolder
where entity.FolderOne == folder.FolderOne &&
entity.FolderTwo == folder.FolderTwo && string.IsNullOrEmpty(entity.FolderThree)
select entity).ToList();
}
else if (item == 3)
{
HavelistFolder = (from entity in listFolder
where entity.FolderOne == folder.FolderOne &&
entity.FolderTwo == folder.FolderTwo && entity.FolderThree == folder.FolderThree
select entity).ToList();
}
return HavelistFolder.Count == 0;
}
如果只需要FolderBE其中几个字段,如下:
新建另一个类:
public class NewFolderBE
{
public string FolderOne
{ get; set; }
public string FolderTwo
{ get; set; }
public string FolderThree
{ get; set; }
}
where entity.FolderOne == folder.FolderOne &&
entity.FolderTwo == folder.FolderTwo && entity.FolderThree == folder.FolderThree
select new NewFolderBE { FolderOne = entity.FolderOne, FolderTwo = entity.FolderTwo, FolderThree = entity.FolderThree };
List<NewFolderBE> newlist = needfolder.ToList();
就OK了