1
List<Student> list = new List<Student>() { new Student(){Name="张同学",Age=11,sex="男"}, new Student(){Name="李同学",Age=12,sex="男"}, new Student(){Name="王同学",Age=13,sex="男"}, new Student(){Name="赵同学",Age=14,sex="男"}, }; List<Teacher> list2 = new List<Teacher>() { new Teacher(){Name="张老师",Age=11,sex="男",position="校长"}, new Teacher(){Name="李老师",Age=22,sex="女",position="教导主任"}, new Teacher(){Name="王老师",Age=23,sex="男",position="年级主任"}, new Teacher(){Name="赵老师",Age=24,sex="女",position="教授"}, }; //多个条件 var a = from m in list join q in list2 on new { m.Age, Name = m.Name,HAHA = m.sex } equals new { q.Age, Name = q.Name, HAHA = q.sex } where m.Name != null select new { m.Name, m.Age }; //一个条件 var b = from m in list join q in list2 on m.Age equals q.Age select m; //类似a var c = from m in list join q in list2 on new { m.Age,m.Name} equals new { q.Age,q.Name} where m.Name != null select new { m.Name, m.Age }; //查询符合条件a表的数据 var d = from m in list join q in list2 on m.Age equals q.Age select m; //获取2个表里面的字段 var e = from m in list join q in list2 on m.Age equals q.Age select new RecentInfo() { Name = m.Name, // time = q == null ? null : q.time, position = q == null ? string.Empty : q.position.ToString() }; var S = a.ToList(); var S2 = b.ToList(); var S3 = c.ToList(); var S4 = d.ToList(); var S5 = e.ToList(); #region 三张表连表查询 //var query = from m in list // join q in list2 on m.Age equals q.Age // join p in list3 on q.Age equals p.Age // select m.Name; //var tmp = query.Distinct().AsEnumerable(); //if (tmp != null && tmp.Any()) // return tmp.ToList(); //return new List<string>(); #endregion // return a.OrderBy(m => m.Name).Skip(pageIndex * pageCount).Take(pageCount).AsEnumerable();//分页
//三张表操作 获取三张表里的字段
var f = from a in A join b in B on a.id equals b.id join c in C on b.id equals c.id into temp from t in temp.DefaultIfEmpty() select new newmodel { newmodel.a = a.name, newmodel.b = b.name, newmodel.c = t.name }
或者;
这种方法最后返回的是三个表所有字段用户自己在调用那个位置选择展示数据
public IEnumerable<T> Get(Func<a类型, b类型, c类型, T> selector) { var rec = from a in 表A join b in 表B on a.字段 equals b.字段 join c in 表C on b.字段 equals c.字段 select selector(a, b, c); return rec; }
调用
var query = Get((a, b, c) => new { id1 = a.id, id2 = b.id, id3 = c.id });