1.对对象集合的简单运用
//定义有实体类A,其中类成员变量都为公有,分别为string field1 和 int field2
//假设List为A的对象集合,即List类型为List<A>,则可以使用Linq语句进行查询
from n in List
where n.field1 == "10" && n.field2 > 5
select n
//以上代码查询结果为查询List对象集合中,成员field1 的值等于字符串"10"且成员field2的值大于数字5的对象集合
2.对DataTable的简单运用
string tempStr = "1,2";
//获取ble中temp列不存在于tempStr字符串中的行
DataTable ble = 获取需要操作的DataTable数据
DataTable temp = (from t in ble.AsEnumerable()
where !tempStr.Contains(t.Field<string>("temp"))
select t).CopyToDataTable();
3.分组的简单运用
Linq分组分两种,⼀种是表分组汇总,另⼀种是List<>分组汇总。下⾯分别举例说明两种分组汇总。
第⼀种,表的分组汇总:
DataTable table = new DataTable();
table.Columns.Add("列名1", Type.GetType("System.Int32"));
table.Columns.Add("列名2", Type.GetType("System.Int32"));
table.Columns.Add("列名3", Type.GetType("System.String"));
table.Columns.Add("列名4", Type.GetType("System.String"));
table.Columns.Add("列名5", Type.GetType("System.String"));
table.Columns.Add("数值或⾦额列名", Type.GetType("System.String"));
//为table表赋值
……
//按[列名2]分组汇总
var dtSummary = from q in table.AsEnumerable()
group q by new { 列名2= q.Field<int>("列名2")} into g
select new
{
列名2= g.Key.列名2,
数值或⾦额汇总列名= g.Sum(a => a.Field<decimal>("数值或⾦额列名"))
};
第⼆种,List<>分组汇总:
var listSummary = from p in List<实体>
group p by new { 属性1 = p.属性1 } into g
select new
{
属性1 = g.Key.属性1 ,
数值或⾦额汇数值或⾦额汇总= g.Sum(a => a.数值或⾦额属性)
};
//p为实体别名,代表List中的实体,
//
int x = 2
var listSummary = (from p in List<实体>
//条件查询List中属性1的值等于x的数据
where p.属性1 == x
//通过p.属性1分组,输出为g
group p by new { 属性1 = p.属性1 } into g
//此处可以输出为类,不写类名为直接输出为object类型,等同于 new {},
select new
{
属性1 = g.Key.属性1 ,
数值或⾦额汇数值或⾦额汇总= g.Sum(a => a.数值或⾦额属性),
数据数量 = g.Count()
}).OrderByDescending(a => a.属性1).ToList();//输出的内容用属性1进行倒序排序并输出为集合,此处需要用小括号将linq内容括起来,才能使用排序和输出集合方法
4.联接的简单运用
有表结构:
id=baseId,需要输出结果:
LINQ代码如下
//表一类
public class table_a{
public int id {get;set;}
public string value {get;set;}
}
//表2类
public class table_b{
public int baseId {get;set;}
public string name{get;set;}
}
var list = from a in List<table_a>
join b in List<table_b> on a.id equals b.baseId
select new
{
id=a.id,
value=a.value,
name = b.name
} ;//list为需要的输出结果
5.联表嵌套分组应用
模拟场景:需要按照县区统计系统内各县区用户数量,新增用户数量(操作日志只有一条的用户)、活跃用户数量(近7天有4天登陆的用户)、留存用户数量(累计3天登陆的用户)。
示例代码:
//用户登录日志类
public class LogLoginEntity
{
public long BaseCreatorId{ get; set; }//用户表主键ID
public DateTime BaseCreateTime{ get; set; }//登录时间
public string ip { get; set; }//登录IP
}
//用户类
public class UserEntity{
public long Id {get;set;}//用户主键ID
public string AreaCode {get;set;}//用户所属行政区划编号
public string RealName {get;set;}//用户真实姓名
}
//行政区划类
public class AreaEntity{
public long Id {get;set;}//主键ID
public string AreaCode {get;set;}//行政区划编号
public string AreaCode {get;set;}//父级行政区划编号
public string AreaName {get;set;}//行政区划名称
}
//用户报表类
private class userReport {
public string Id { get; set; }
public string Name { get; set; }//用户姓名
public List<GroupInfo> GroupData { get; set; }//日志分组信息
}
//分组信息类
public class treeInfo
{
public string text { get; set; }//登陆时间
public string value { get; set; }//登陆次数
}
//业务代码
public object GetReportData()
{
object result;
try
{
//获取系统用户列表
List<UserEntity> userList = GetUserList()//自行编写获取集合方法
//获取用户登录日志列表
List<LogLoginEntity > loginLogList = GetLoginLogList()//自行编写获取集合方法
//获取行政区划列表
List<AreaEntity> areaList = GetAreaList()//自行编写获取集合方法
//用户数量
List<string> userCount = new List<string>();
//新增用户(操作日志只有一条的用户)
List<string> newUserCount = new List<string>();
//活跃用户(近7天有4天登陆的用户)
List<string> activeUserCount = new List<string>();
//留存用户(累计3天登陆的用户)
List<string> retainedUserCount = new List<string>();
DateTime date = DateTime.Now.AddDays(-6);//从当前日期算起取7天前的日期
//循环行政区划列表
areaList.ForEach(a => {
//获取所属当前行政区划用户列表
List<UserEntity> tempUserList = userList.Where(user => user.AreaCode == a.AreaCode).ToList();
//添加当前地区用户数量
userCount.Add(tempUserList.Count.ToString());
//Linq查询数据
List<userReport> tempNewUserList = (from log in loginLogList//以日志表为主表
join user in tempUserList on log.BaseCreatorId equals user.Id//联接用户表通过主外键
where user.AreaCode == a.AreaCode//获取联表数据中所属当前行政区划数据
group new {
//创建分组集合信息,由用户真实姓名和登陆时间组成
RealName = user.RealName,
BaseCreateTime = log.BaseCreateTime
} by new { Id = user.Id//用用户ID对数据进行分组 } into g
select new userReport()
{//创建报表类实体
Id = g.Key.Id.ToString(),//用来分组的字段
Name = g.Max(r => r.RealName),//分组集合g姓名最大值(姓名相同,通过方法获取,类似SQL中的聚合函数)
//对当前用户的分组集合g再次进行分组
GroupData = (from groupMod in g
group groupMod by new { LoginTime = ((DateTime)groupMod.BaseCreateTime).ToString("yyyy-MM-dd") } into g1//使用BaseCreateTime的字符串格式进行分组,
select new treeInfo()
{
text = g1.Key.LoginTime,//用来分组的字段
value = g1.Count().ToString()//分组集合g1数据数量
}).ToList()
}).ToList();
//添加新增用户数量,通过查询用户日志分组信息数量如果只有一条数据判断为新增用户
newUserCount.Add(tempNewUserList.Where(user => user.GroupData.Count == 1).Count().ToString());
//添加活跃用户数量,通过查询用户的日志分组信息中登陆时间日期大于设定日期的数据数量大于等于4判断为活跃用户
activeUserCount.Add(tempNewUserList.
Where(user => user.GroupData.
Where(group => DateTime.Parse(group.text) >= date).Count() >= 4).Count().ToString());
//添加留存用户数量,通过查询用户的日志分组信息中数据数量大于等于3判断为留存用户
retainedUserCount.Add(tempNewUserList.Where(user => user.GroupData.Count >= 3).Count().ToString());
});
result= new {
totalCount = userList.Count,
userCount = userCount,
newUserCount = newUserCount,
activeUserCount = activeUserCount,
retainedUserCount = retainedUserCount,
areaList = areaList
};
}
catch (Exception ex)
{
}
return (result);//返回结果
}