List(T) 的实现实体对象操作,之Find FindAll Contains ForEach Sort
实体对象类:
新闻发布实体类
[MapTo("us_News")]
public abstract class NewsBLL
{
[MapTo("FId")]
public abstract string Id { get;}
/// <summary>
/// 标题
/// </summary>
[MapTo("FCaption")]
public abstract string Caption {get; set;}
/// <summary>
/// 新闻类别PK-ss_Item.ID
/// </summary>
[MapTo("FType")]
[ManyToOne(LocalKey = "FType", ForeignKey = "FId")]
public abstract DictItemBLL Type {get; set;}
/// <summary>
/// 新闻内容
/// </summary>
[MapTo("FContent")]
public abstract string Content {get; set;}
/// <summary>
/// 发布人
/// </summary>
[MapTo("FUserID")]
public abstract string UserID {get; set;}
}
新闻类别类
[MapTo("ss_Item")]
public abstract class DictItemBLL
{
[MapTo("FId")]
public abstract int Id { get;}
/// <summary>
/// 分组编号
/// </summary>
[MapTo("FClassID")]
public abstract int ClassID {get; set;}
/// <summary>
/// 父节点ID
/// </summary>
[MapTo("FParentID")]
public abstract int ParentID {get; set;}
/// <summary>
/// 字典名称
/// </summary>
[MapTo("FName")]
public abstract string Name {get; set;}
#endregion
}
[MapTo("us_News")]
public abstract class NewsBLL
{
[MapTo("FId")]
public abstract string Id { get;}
/// <summary>
/// 标题
/// </summary>
[MapTo("FCaption")]
public abstract string Caption {get; set;}
/// <summary>
/// 新闻类别PK-ss_Item.ID
/// </summary>
[MapTo("FType")]
[ManyToOne(LocalKey = "FType", ForeignKey = "FId")]
public abstract DictItemBLL Type {get; set;}
/// <summary>
/// 新闻内容
/// </summary>
[MapTo("FContent")]
public abstract string Content {get; set;}
/// <summary>
/// 发布人
/// </summary>
[MapTo("FUserID")]
public abstract string UserID {get; set;}
}
新闻类别类
[MapTo("ss_Item")]
public abstract class DictItemBLL
{
[MapTo("FId")]
public abstract int Id { get;}
/// <summary>
/// 分组编号
/// </summary>
[MapTo("FClassID")]
public abstract int ClassID {get; set;}
/// <summary>
/// 父节点ID
/// </summary>
[MapTo("FParentID")]
public abstract int ParentID {get; set;}
/// <summary>
/// 字典名称
/// </summary>
[MapTo("FName")]
public abstract string Name {get; set;}
#endregion
}
1,使用Find搜索单个匹配值
NewsBLL news = list.Find(delegate(NewsBLL x)
{
return x.Type.Id == 10001; //搜索新闻列表中类别(Type)子对象中的 Id=10001的单个对象
});
{
return x.Type.Id == 10001; //搜索新闻列表中类别(Type)子对象中的 Id=10001的单个对象
});
2,使用FindAll搜索多个匹配值
List<NewsBLL> newsList = list.FindAll(delegate(NewsBLL x)
{
return x.Type.Id == 10001; //搜索新闻列表中类别(Type)子对象中的 Id=10001的多个对象
});
{
return x.Type.Id == 10001; //搜索新闻列表中类别(Type)子对象中的 Id=10001的多个对象
});
3,是用Contains检查满足条件的值是否存在
bool isContains= list.Contains(delegate(NewsBLL x)
{
return x.Type.Id == 10001 && x.UserID=="ejiyuan"; //搜索新闻列表中类别(Type)子对象中的 Id=10001,并且发布人等于ejiyuan的是否存在
});
{
return x.Type.Id == 10001 && x.UserID=="ejiyuan"; //搜索新闻列表中类别(Type)子对象中的 Id=10001,并且发布人等于ejiyuan的是否存在
});
4,使用ForEach 对每个列表对象进行操作
list.ForEach(delegate(NewsBLL x)
{
x.Caption = "10001"; //将列表中所有标题都修改为10001
});
{
x.Caption = "10001"; //将列表中所有标题都修改为10001
});
5,使用sort排序,按类别 id排序
list.Sort(delegate(NewsBLL x,DocumentBLL y)
{
if (x.Type.Id < y.Type.Id )
{
return -1;
}
else if (x.Type.Id == y.Type.Id )
{
return 0;
}
else return 1;
});
{
if (x.Type.Id < y.Type.Id )
{
return -1;
}
else if (x.Type.Id == y.Type.Id )
{
return 0;
}
else return 1;
});