Linq Group by获取数量和数据
主表:
public partial class Activity
{
[Key]
public int pkActivity { get; set; }
public int fkEmployee { get; set; }
public string ActivityName { get; set; }
public DateTime RegistrationStartDateTime { get; set; }
public DateTime RegistrationEndDateTime { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public int MaxNumOfPeople { get; set; }
public decimal PricePerPeople { get; set; }
public string ActivityLocation { get; set; }
public string ActivityStatus { get; set; }
public string Description { get; set; }
public string ImgUrl { get; set; }
public DateTime CreateOn { get; set; }
public string CreateBy { get; set; }
public DateTime LastModifyOn { get; set; }
public string LastModifyBy { get; set; }
}
重表:
public partial class ActivityEmployee
{
[Key]
public int pkActivityEmployee { get; set; }
public int fkActivity { get; set; }
public int fkEmployee { get; set; }
public string Comments { get; set; }
public DateTime CreateOn { get; set; }
public string CreateBy { get; set; }
}
获取当前活动注册人数linq 写法,用DefaultIfEmpty 表示left join,通过group by new 进行分组,并通过groupedTable.Key.属性 提取数据:
/// <summary>
/// 获取我创建的活动列表。
/// </summary>
/// <param name="employeeKey"></param>
/// <returns></returns>
public IEnumerable<ActivityDTO> GetMyCreatedActivitiesList(int employeeKey)
{
var query = from activity in clientDbContext.Activity
join activityEmployee in clientDbContext.ActivityEmployee
on activity.pkActivity equals activityEmployee.fkActivity into temp
from t in temp.DefaultIfEmpty()
where activity.fkEmployee == employeeKey
group t by new
{
activity.pkActivity,
activity.fkEmployee,
activity.ActivityName,
activity.RegistrationStartDateTime,
activity.RegistrationEndDateTime,
activity.StartDateTime,
activity.EndDateTime,
activity.ImgUrl,
activity.ActivityStatus,
activity.ActivityLocation,
activity.Description,
activity.CreateOn,
activity.CreateBy,
activity.LastModifyBy,
activity.LastModifyOn
}
into groupActivity
select new ActivityDTO()
{
Id = groupActivity.Key.pkActivity,
CreatedEmployeKey = groupActivity.Key.fkEmployee,
ActivityName = groupActivity.Key.ActivityName,
RegistrationStartDateTime = groupActivity.Key.RegistrationStartDateTime,
RegistrationEndDateTime = groupActivity.Key.RegistrationEndDateTime,
StartDateTime = groupActivity.Key.StartDateTime,
EndDateTime = groupActivity.Key.EndDateTime,
ActivityImageUrl = groupActivity.Key.ImgUrl,
ActivityStatus = groupActivity.Key.ActivityStatus,
ActivityLocation = groupActivity.Key.ActivityLocation,
Description = groupActivity.Key.Description,
CreateOn = groupActivity.Key.CreateOn,
CreateBy = groupActivity.Key.CreateBy,
LastModifyBy = groupActivity.Key.LastModifyBy,
LastModifyOn = groupActivity.Key.LastModifyOn,
RegisteredCount = groupActivity.Count(g=>g.fkActivity!=null)//报名人数,left join存在null数据
}; return query;
}
Linq Group by获取数量和数据的更多相关文章
- 用LINQ获取XML节点数据
Insus.NET想对<从字符串中获取XML节点数据> http://www.cnblogs.com/insus/p/3299052.html 这篇改写为使用LINQ的方法实现.LINQ中 ...
- WindowsPhone 在 根据公历 获取月球日期数据
WindowsPhone 在 根据公历 获取月球日期数据 WindowsPhone 在 它们的定义 类,根据公历 获取月球日期数据 using System; using System.Collect ...
- 使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置)
原文:使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置) 在上一篇中说到了Silverlight下的Socket通信,在最后的时候说到本篇将会结合地图. ...
- 以多进程读取oss符合条件的数据为例,综合使用多进程间的通信、获取多进程的数据
import datetime import sys import oss2 from itertools import islice import pandas as pd import re im ...
- C#使用Linq to csv读取.csv文件数据
前言:今日遇到了一个需要读取CSV文件类型的EXCEL文档数据的问题,原本使用NPOI的解决方案直接读取文档数据,最后失败了,主要是文件的类型版本等信息不兼容导致.其他同事有使用linq to csv ...
- 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。
最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...
- 读取TDrawGrid之获取博易数据
朋友叫我帮忙写个从博易读取数据的工具,可无奈数据所在控件并不是Windows标准控件,也就是说没办法通过发送系统消息来获取 相关数据,于是乎试了一下从内存直接读取,可最后并不能达到预期目的,原因是笔者 ...
- C#中,使用正式表达式匹配获取所需数据
.NET中,使用正式表达式匹配获取所需数据 需求:获取一串字符串中,正则匹配出需要的数据. 例如以下字符串: string temp ="ErrorCode:-1,Message:{&quo ...
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
[源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...
随机推荐
- wpf 程序启动显示图片
一.设置图片的生成操作 程序启动时会出现0.5秒的图片显示,再显示程序界面. 二.写代码实现相同效果 /// <summary> /// App.xaml 的交互逻辑 /// </s ...
- [Python]Python日期格式和字符串格式相互转换
由字符串格式转化为日期格式的函数为: datetime.datetime.strptime() 由日期格式转化为字符串格式的函数为: datetime.datetime.strftime() # en ...
- linux学习笔记1:linux驱动设备概述
- 如何在Word中排出漂亮的代码,去除回车符,去除拼写检查
这位博主写到很到位,这里补充一下在VBA里用模块的部分. https://blog.csdn.net/code4101/article/details/41802715 1.放代码的方式是贴纯文本. ...
- [JSOI2016]最佳团体 DFS序/树形DP
题目 洛谷 P4322 [JSOI2016]最佳团体 Description 茜茜的舞蹈团队一共有\(N\)名候选人,这些候选人从\(1\)到\(N\)编号.方便起见,茜茜的编号是\(0\)号.每个候 ...
- [CF653F] Paper task - 后缀数组,线段树,vector
[CF653F] Paper task Description 给定一个括号序列,统计合法的本质不同子串的个数. Solution 很容易想到,只要在传统统计本质不同子串的基础上修改一下即可. 考虑经 ...
- PP: UMAP: uniform manifold approximation and projection for dimension reduction
From Tutte institute for mathematics and computing Problem: dimension reduction Theoretical foundati ...
- MS yc
# word - operate标题栏 菜单栏 工具栏 页面 状态栏 字体阴影 背景色 着重号 项目符号 数字编码 格式刷
- Elasticsearch系列---倒排索引原理与分词器
概要 本篇主要讲解倒排索引的基本原理以及ES常用的几种分词器介绍. 倒排索引的建立过程 倒排索引是搜索引擎中常见的索引方法,用来存储在全文搜索下某个单词在一个文档中存储位置的映射.通过倒排索引,我们输 ...
- Mysql5.6基础命令
Centos7下mysql5.6数据库的操作 Mysql如何修改密码? 1.使用mysqladmin修改,这种修改方式需要知道mysql的原始密码 修改密码后我们测试下看看能不能登录成功 怎么才能不需 ...