日期:2014-05-17  浏览次数:20476 次

"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.cs 59 62 Mycems.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.cs 60 91 Mycems.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><