"Linq to object"实现以及错误排除
首先感谢“q107770540”等网友的帮助。
一、"Linq to object"方法实现
public List<ListMenuCatalog> GetRoomLayerBuildingLists()
{
List<ListMenuCatalog> listmenucatalogs = new List<ListMenuCatalog>();
List<RoomLayerBuilding> roomlayerbuildings = this.GetRoomLayerBuildingDatas();
//var listbybuilding = roomlayerbuildings.Distinct(r => r.BuildingNo).ToList();
[color=#FF0000]问题一:“roomlayerbuildings.Distinct(r => r.BuildingNo).ToList()”这样不对么,为什么会出现这样的错误,信息如下?
错误1无法将 lambda 表达式 转换为类型“System.Collections.Generic.IEqualityComparer<ServicesEntity.RoomLayerBuilding>”,因为它不是委托类型C:\Users\Administrator\Desktop\DX4.0\Mycems.BLL\RoomLayerBuildingDatas.cs5962Mycems.BLL
[/color]
//var listbybuilding = (from lb in roomlayerbuildings select roomlayerbuildings.BuildingNo).Distinct();
问题二:“from lb in roomlayerbuildings select roomlayerbuildings.BuildingNo).Distinct()”这样写不对么? 为什么会出现这样的错误,信息如下?
错误1“System.Collections.Generic.List<ServicesEntity.RoomLayerBuilding>”不包含“BuildingNo”的定义,并且找不到可接受类型为“System.Collections.Generic.List<ServicesEntity.RoomLayerBuilding>”的第一个参数的扩展方法“BuildingNo”(是否缺少 using 指令或程序集引用?)C:\Users\Administrator\Desktop\DX4.0\Mycems.BLL\RoomLayerBuildingDatas.cs6091Mycems.BLL
return listmenucatalogs;
}
问题三:如果从“roomlayerbuildings”集合中筛选出“DpRoomNo”、“RoomName”两个属性字段不重复的的结果集,应该怎样写?
问题四:“var query3=room.Where(r=>r.DpRoomNo=="001").ToList();”返回的是一个“List”,要获取其中的值,还要将“Var query3”转化为“List”集合,然后获取第一个是么?还有其它好的方法么?
完整实体类:
public class RoomLayerBuilding : INotifyPropertyChanged
{
public RoomLayerBuilding() { }
/// <summary>
/// 房间编号
/// </summary>
public String DpRoomNo
{
get
{
return _dproomno;
}
set
{
_dproomno = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("DpRoomNo"));
}
}
/// <summary>
/// 房间名称
/// </summary>
public String RoomName
{
get
{
return _dproomname;
}
set
{
_dproomname = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("RoomName"));
}
}
/// <summary>
/// 楼层编号
/// </summary>
public String LayerNo
{
get
{
return _dplayerno;
}
set
{
_dplayerno = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("LayerNo"));
}
}
/// <summary>
/// 建筑编号
/// </summary>
public String BuildingNo
{
get
{
return _dpbuildingno;
}
set
{
_dpbuildingno = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("BuildingNo"));
}
}
/// <summary>
/// 建筑名称
/// </summary>
public String BuildingName
{
get
{
return _dpbuildingname;
}
set
{
_dpbuildingname = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("BuildingName"));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
String _dproomno;
String _dproomname;
String _dplayerno;
String _dpbuildingno;
String _dpbuildingname;
}
[解决办法]
1.vroomlayerbuildings.Distinct(r => r.BuildingNo).ToList();
返回的是由BuildingNo值组成的int数组(BuildingNo类型是int)
你如果想返回ListMenuCatalog类型的数组,可以这样:
var listbybuilding = roomlayerbuildings.GroupBy(r => r.BuildingNo).Select(g=>g.First()).ToList();
[解决办法]
//问题2var listbybuilding = (from lb in roomlayerbuildings select lb.BuildingNo).Distinct();//其实等价于:var listbybuilding = roomlayerbuildings.Select(lb=> lb.BuildingNo).Distinct();问题3: 参考 http://blog.csdn.net/q107770540/article/details/5784646问题4: 如果只想获取第一个,可以这样:var query3=room.FirstOrDefault(r=>r.DpRoomNo=="001");
[解决办法]
http://topic.csdn.net/u/20101111/09/ea049af5-d079-4a13-8c1d-e1a3350a830d.html