Linq有很多值得学习的地方,这里我们主要介绍Linq Setting字段,包括介绍Linq中Join连所子查询结合等方面。
Linq Setting字段存储
本文介绍Linq Setting字段,这个用过Linq的人都知道它的好处,如在设计一张表如学生信息表,如学习爱好,性格,生活经历等这些字段,相信大家在以前的设计理念是将三个字段设计成三个 Nvarchar(8000)类型字段,或者有些设计成 Text 类型字段,这些设计都没错,但都不是很好。
现在有 Linq,可以将这三个字段都放在Linq Setting字段中(注意这里,字段的名称必须为Setting),Linq Setting字段设置成 image 类型即可实现数据的存储。如何用C#语法来存储呢?????以后有空再说。用Setting字段存储的数据,缺点是做条件查询时,用Setting作为查询条件就有些复杂了,一般不作条件查询。
- private string _hotelDescription = string.Empty;//兴趣
- public string HotelDescription { get { return _hotelDescription;
- }
- set { _hotelDescription = value; } }
- partial void OnLoaded()
- {
- byte[] bytes = Setting.ToArray();
- CSetting setting = new CSetting(bytes);
- load(setting);
- }
- partial void OnValidate(System.Data.Linq.ChangeAction action)
- {
- CSetting setting = new CSetting();
- save(setting);
- byte[] bytes = setting.ToByteArray();
- Setting = new Binary(bytes);
- }
- private void save(CSetting setting)
- {
- setting.Set("desc", _hotelDescription);
- }
- private void load(CSetting setting)
- {
- _hotelDescription = setting.Get("desc", string.Empty);
- }
Linq中的Union(递归查询,查询父节点下所有节点)
- var query = (from item1 in dc.SysOrganization
- where (item1.FatherSysOrganizationID == orgId) && item1.IsDel == false
- select new SysOrganizationInfo { SysOrganizationID =item1.SysOrganizationID,
Name=item1.Name,Code=item1.Code,BaseProvinceID=(int)item1.BaseProvinceID,
BaseCityID=(int)item1.BaseCityID})- .Union(
- from item2 in dc.SysOrganization
- join item3 in dc.SysOrganization
- on item2.FatherSysOrganizationID equals item3.SysOrganizationID
- where (item2.SysOrganizationID == orgId) && item2.IsDel == false
- select new SysOrganizationInfo { SysOrganizationID = item2.SysOrganizationID,
Name = item2.Name, Code = item2.Code, BaseProvinceID = (int)item2.BaseProvinceID,
BaseCityID = (int)item2.BaseCityID });
Linq中Join连所子查询结合
- var roles = from aa in(
- from a in dc.SysOrganizationRoleUser
- join b in dc.SysOrganizationRole on a.SysRoleID equals b.SysRoleID
- join c in dc.SysOrganizationRoleFunctionItem on b.SysRoleID equals c.SysRoleID
- join d in dc.SysFunctionItem on c.SysFunctionItemID equals d.SysFunctionItemID
- join d2 in dc.SysFunctionItem on c.SysFunctionItemID equals d2.SysFunctionItemID
- where a.SysUserID == userID
- select d)
- from bb in dc.SysFunctionItem
- where aa.SysFunctionItemID == bb.SysFunctionItemID ||
aa.FatherFunctionItemID == bb.SysFunctionItemID- select bb;
Linq中的左连接查询用到 DefaultIfEmpty() 语法
- var svcUser = from a in hdc.SysServiceCenterUser
- join b in hdc.SysServiceCenter on a.SysServiceCenterID equals b.SysServiceCenterID
- join c in hdc.SysServicePositionUser on a.SysUserID equals c.SysUserID into pous
- from c in pous.DefaultIfEmpty()
- join d in hdc.SysServiceCenterPosition on c.SysServicePositionID
equals d.SysServicePositionID into po- from d in po.DefaultIfEmpty()
- where (_SysServiceCenterID == 0 ? true : a.SysServiceCenterID == _SysServiceCenterID)
- && (_SysServicePositionID == 0 ? true : (from w in hdc.SysServicePositionUser where
w.SysServicePositionID == _SysServicePositionID select w.SysUserID).Contains(a.SysUserID))- && (_Name == null || _Name.Trim() == "" ? true : a.FullName.Contains(_Name))
- && (_Code == null || _Code.Trim() == "" ? true : a.Code == _Code)
- select new Svc
- {
- _SysUserID = (a.SysUserID == null ? 0 : a.SysUserID),
- _SysServiceCenterID = (a.SysServiceCenterID==null?0:(int)a.SysServiceCenterID),
- //_SysServicePositionID = (int)c.SysServicePositionID,
- _SvcName = b.Name,
- //_PositionName = d.Name,
- _LoginName = a.LoginName,
- _FullName = a.FullName,
- _Code = (a.Code==null?"":a.Code)
- };
【编辑推荐】