日期:2014-05-20  浏览次数:21035 次

LINQ三表联合查询之后数据怎么合并?
一个用户表。一个部门表。一个考勤表。
代码如下[code=C#][/code]





 protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  //查询部门信息
  this.DropDownList1.DataTextField = "DepartName";
  this.DropDownList1.DataValueField = "DepartId";
  this.DropDownList1.DataSource = DAL.DepartInfo.Depart();
  DropDownList1.DataBind();
  DateTime a = DateTime.Now.AddDays(7);
  DAL.DataClassesDataContext db = new DAL.DataClassesDataContext();
  var q = from User in db.UserInfo
  join De in db.DepartInfo on User.DepartId equals De.DepartId
  join Ma in db.ManualSign on User.UserId equals Ma.UserId
  select new { 姓名 = User.UserName, 迟到次数 = chidao(User.UserName), 早退次数 = zaotui(User.UserName), 旷工次数=kuanggong(User.UserName) ,所属部门=De.DepartName };
  GridView1.DataSource = q.ToList();
  GridView1.DataBind();
  }
  }
//查询指定用户的迟到次数
  public int chidao(string a)
  {
  DAL.DataClassesDataContext db = new DAL.DataClassesDataContext();
  var z = from x in db.WorkTime
  select x;
  DAL.DataClassesDataContext dd = new DAL.DataClassesDataContext();
  var q = from w in dd.ManualSign
  where w.SignTime > DateTime.Parse(z.First().OnDutyTime) && w.SignTag == 1&&w.UserInfo.UserName==a
  select w;
  return q.Count();
  }
//查询指定用户的早退次数
  public int zaotui(string a)
  {
  DAL.DataClassesDataContext db = new DAL.DataClassesDataContext();
  var z = from x in db.WorkTime
  select x;
  DAL.DataClassesDataContext dd = new DAL.DataClassesDataContext();
  var q = from w in dd.ManualSign
  where w.SignTime < DateTime.Parse(z.First().OffDutyTime) && w.SignTag == 0&&w.UserInfo.UserName==a
  select w;
  return q.Count();
  }
//查询指定用户的旷工次数
  public int kuanggong(string aa)
  {
  DateTime qishi = DateTime.Parse(TextBox1.Text.Trim());
  DateTime jieshu = DateTime.Parse(TextBox2.Text.Trim());
  int a = System.Data.Linq.SqlClient.SqlMethods.DateDiffDay(qishi, jieshu);
  int p = 0;
  int o = 0;
  DAL.DataClassesDataContext db = new DAL.DataClassesDataContext();
  var z = from x in db.WorkTime
  select x;
  DAL.DataClassesDataContext dd = new DAL.DataClassesDataContext();
  for (int b = 0; b <= a; b++)
  {
  var q = from w in dd.ManualSign
  where w.SignTime == qishi.AddDays(b) && w.SignTag == 1&&w.UserInfo.UserName==aa
  select w;
  if (q.Count() != 0)
  {
  p = p + 1;
  }
  }
  o = a - p;
  return o;

  }

我这样能查出数据。但是旷工次数是我输入的时间天数总和。

在旷工方法里面数据没有查出上班天数。


这是查询出来的东西



希望有人能给解答一下。。。。

------解决方案--------------------
试试条件 把 用户名都放前面, 把循环去掉... 效率会不会高一些.
另外 这段 
for (int b = 0; b <= a; b++)
{
var q = from w in dd.ManualSign
where w.SignTime == qishi.AddDays(b) && w.SignTag == 1&&w.UserInfo.UserName==aa