“Linq”操作对象集合
一、“Linq”操作对象集合:public class Room
{
public Room() { }
/// <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"));
}
}
}
二、对象集合 List<Room> room=new List<Room>()
问题一:如何对“room”对象进行操作,筛选出“DpRoomNo”属性不同的对象,作为新的“List<Room>”对象???(Sql里面可以“distinct”来实现)
问题二:如何对“room”对象进行遍历,“Foreach (var r in room)”对么?
问题三:如何从“room”对象中筛选“DpRoomNo”为“001”的记录?
问题四:如何从“room”对象中排除“RoomName”属性? --------------------编程问答--------------------
--------------------编程问答--------------------
List<Room> room=new List<Room>();
//1.
var query1=room.Distinct(r=>r.DpRoomNo).ToList();
//2.
room.ForEach(r=>Console.WriteLine(r.DpRoomNo));
//3.
var query3=room.Where(r=>r.DpRoomNo=="001").ToList();
//4.
var query4=room.Select(r=>r.DpRoomNo).ToList();
谢谢您。。。
问题二:“room.ForEach(r=>Console.WriteLine(r.DpRoomNo));” 没有办法获取“r.DpRoomNo”的值并将其写入“数组arry”中啊,是不是还要用“Foreach”???
问题四:“var query4=room.Select(r=>r.DpRoomNo).ToList();”这样只是获取“DpRoomNo”属性,并不能排除“DpRoomName”属性啊?
如果“Room”对象有“RoomNo”、“RoomName”、“RoomNum”三个属性,现在想从中排除“RoomName”属性应该怎么办呢? --------------------编程问答-------------------- 问题2:
int[] result=room.Select(r=>r.DpRoomNo).ToArray();
问题4:
RoomName已经在类定义就定义好的属性,怎么排除?
只有查询你想要字段值,不查询 RoomName即可
--------------------编程问答-------------------- 学习下 linq .linq to object 感觉还不错. --------------------编程问答--------------------
Linq 为什么那么方便呢?
有“Linq to object”、“Linq to SQL”、“Linq to EF”,还有其它的么? --------------------编程问答-------------------- linq to dataset --------------------编程问答-------------------- 找到了。LINQ to Objects、LINQ to DataSets、LINQ to SQL、LINQ to Entities、LINQ to XML。。。
谢谢您。。。
找到了,但是不对啊。两种方法。。。
LINQ to Objects、LINQ to DataSets、LINQ to SQL、LINQ to Entities、LINQ to XML
问题一:“var listbybuilding = 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
问题二:“var listbybuilding = (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
问题三:如果根据“BuildingNo”和“BuildingName”两个属性字段进行筛应该怎么办? --------------------编程问答-------------------- 完整的实体类:
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;
} --------------------编程问答-------------------- public List<ListMenuCatalog> GetRoomLayerBuildingLists()
{
List<ListMenuCatalog> listmenucatalogs = new List<ListMenuCatalog>();
List<RoomLayerBuilding> roomlayerbuildings = this.GetRoomLayerBuildingDatas();
//var listbybuilding = roomlayerbuildings.Distinct(r => r.BuildingNo).ToList();
//var listbybuilding = (from lb in roomlayerbuildings select roomlayerbuildings.BuildingNo).Distinct(); --------------------编程问答-------------------- var listbybuilding = roomlayerbuildings.Select(r => r.BuildingNo).Distinct().ToList();”
Or:
var listbybuilding = (from lb in roomlayerbuildings select lb.BuildingNo).Distinct(); --------------------编程问答-------------------- http://msdn.microsoft.com/zh-cn/library/bb341635.aspx
lz先看看这个吧
无论xml、obj、Entities都是继承了IEnumerable接口,方法都是一样的
--------------------编程问答--------------------
不是没人啊,周么,下了下。
详细见:这里
--------------------编程问答-------------------- 有关于“Linq”函数方法的资料么? --------------------编程问答--------------------
谢谢您。
期望:
1、从结果集中筛选出“BuildingNo”不重复的记录。
2、筛选出来的每条记录的“BuildingName”属性用于实例化“ListMenuCatalog”实体集的“CatalogName ”属性。而它的“SubitemName”属性下面说。
3、“ListMenuCatalog”实体集的“SubitemName”属性,要根据其“CatalogName ”属性,从“roomlayerbuildings”(源数据集)中筛选出“RoomNo”和“RoomName”作为“ListMenuSubitem”对象集合,用于实例化“ListMenuCatalog”实体集的“SubitemName”属性。
4、将“ListMenuCatalog”实体集返回。 --------------------编程问答-------------------- 你思路不是很明确了么? --------------------编程问答-------------------- var roomlayerbuildings = this.GetRoomLayerBuildingDatas().Select(rlb => rlb.BuildingNo).Distinct().ToList();
这样是不是只返回“BuildingNo”字段属性的值? --------------------编程问答-------------------- var roomlayerbuildings = this.GetRoomLayerBuildingDatas().Select(rlb => rlb.BuildingNo).Distinct().ToList();
foreach (var item in roomlayerbuildings)
{
ListMenuCatalog lmc = new ListMenuCatalog();
lmc.CatalogName = item.BuildingName;
}
问题:提示“string”不包含"BuildingName"的定义,为什么???
错误信息如下:
错误 1 “string”不包含“BuildingName”的定义,并且找不到可接受类型为“string”的第一个参数的扩展方法“BuildingName”(是否缺少 using 指令或程序集引用?) C:\Users\Administrator\Desktop\DX4.0\Mycems.BLL\RoomLayerBuildingDatas.cs 66 40 Mycems.BLL --------------------编程问答-------------------- 谢谢大家,问题解决了。 --------------------编程问答-------------------- "Dictionary"和"HashTalbe"实现了“IEqualityComparer”
GetHashCode返回的什么???他的作用是什么???
补充:.NET技术 , ASP.NET