Linq 多个左连接 怎么写? ##################################

t101lian 2014-07-11 11:15:13
select a.* ,c.Class_name from 
(
select a.NickName,b.* from UserTb a
left join SpecialTreatmentTb b on a.userid=b.userid
) a

left join GoodsTb c on a.goodid=c.id


请教各位大侠,
这句SQL ,用Linq 怎么写?
回复后再加分!

...全文
847 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
t101lian 2014-07-15
  • 打赏
  • 举报
回复
引用 18 楼 geyewei 的回复:
[quote=引用 16 楼 t101lian 的回复:] 大神帮我再看看, 试过了, 还是不行, 第一处加红的地方就报错了, ,g 不行。 ,
哦,那就把select s,g改成select new { s = s, g = g}吧。 [/quote] 谢谢大神, !!
t101lian 2014-07-14
  • 打赏
  • 举报
回复
自己顶一下
t101lian 2014-07-14
  • 打赏
  • 举报
回复
引用 13 楼 geyewei 的回复:
var lowNums = from q in uInfoList join m in ( from s in SpecialTreatmentInfoLsit join g in GinfoList on s.GoodId equals g.ID select s,g ) on q.UserId equals m.s.UserId into Stords from endStords in Stords.DefaultIfEmpty() select new { q.UserId, q.NickName, name = endStords == null ? "" : endStords.g.name, q.Address, FeeRate = endStords == null ? 0 : endStords.s.FeeRate, BailRate = endStords == null ? 0 : endStords.s.BailRate, SellBailRate = endStords == null ? 0 : endStords.s.SellBailRate, SellFeeRate = endStords == null ? 0 : endStords.s.SellFeeRate, }
大神帮我再看看, 试过了, 还是不行, 第一处加红的地方就报错了, ,g 不行。 ,
t101lian 2014-07-14
  • 打赏
  • 举报
回复
引用 12 楼 u010349035 的回复:
endStords.name
试过,没用的 endStords只有SpecialTreatmentInfoLsit 的字段,
geyewei 2014-07-14
  • 打赏
  • 举报
回复
引用 16 楼 t101lian 的回复:
[quote=引用 13 楼 geyewei 的回复:] var lowNums = from q in uInfoList join m in ( from s in SpecialTreatmentInfoLsit join g in GinfoList on s.GoodId equals g.ID select s,g ) on q.UserId equals m.s.UserId into Stords from endStords in Stords.DefaultIfEmpty() select new { q.UserId, q.NickName, name = endStords == null ? "" : endStords.g.name, q.Address, FeeRate = endStords == null ? 0 : endStords.s.FeeRate, BailRate = endStords == null ? 0 : endStords.s.BailRate, SellBailRate = endStords == null ? 0 : endStords.s.SellBailRate, SellFeeRate = endStords == null ? 0 : endStords.s.SellFeeRate, }
大神帮我再看看, 试过了, 还是不行, 第一处加红的地方就报错了, ,g 不行。 ,[/quote] 哦,那就把select s,g改成select new { s = s, g = g}吧。
Rajesh_James 2014-07-11
  • 打赏
  • 举报
回复
var items = from a in db.UserTb join b in db. SpecialTreatmentTb 
on a.userid equals b.userid select new { a.NickName, b};
var res = from a in items join b in db.GoodsTb  on a.goodid equals b.id select new {a,b.classname};
没有在编译器中跑,只是为了给楼主一个思路,希望对楼主有用
geyewei 2014-07-11
  • 打赏
  • 举报
回复
var lowNums = from q in uInfoList join m in ( from s in SpecialTreatmentInfoLsit join g in GinfoList on s.GoodId equals g.ID select s,g ) on q.UserId equals m.s.UserId into Stords from endStords in Stords.DefaultIfEmpty() select new { q.UserId, q.NickName, name = endStords == null ? "" : endStords.g.name, q.Address, FeeRate = endStords == null ? 0 : endStords.s.FeeRate, BailRate = endStords == null ? 0 : endStords.s.BailRate, SellBailRate = endStords == null ? 0 : endStords.s.SellBailRate, SellFeeRate = endStords == null ? 0 : endStords.s.SellFeeRate, }
-烟花雨季 2014-07-11
  • 打赏
  • 举报
回复
endStords.name
t101lian 2014-07-11
  • 打赏
  • 举报
回复
引用 9 楼 u010349035 的回复:
这个是主表SpecialTreatmentTb 另外两个是从表,其余两个表都跟SpecialTreatmentTb 这个有关系么
大神帮帮看看这个吧 var lowNums = from q in uInfoList join m in ( from s in SpecialTreatmentInfoLsit join g in GinfoList on s.GoodId equals g.ID select s ) on q.UserId equals m.UserId into Stords from endStords in Stords.DefaultIfEmpty() select new { q.UserId, q.NickName, 这里要加上GinfoList中的一个字段怎么写? (g.name 不行呢) q.Address, FeeRate = endStords == null ? 0 : endStords.FeeRate, BailRate = endStords == null ? 0 : endStords.BailRate, SellBailRate = endStords == null ? 0 : endStords.SellBailRate, SellFeeRate = endStords == null ? 0 : endStords.SellFeeRate, }
t101lian 2014-07-11
  • 打赏
  • 举报
回复
引用 9 楼 u010349035 的回复:
这个是主表SpecialTreatmentTb 另外两个是从表,其余两个表都跟SpecialTreatmentTb 这个有关系么
上面SQL 语句中 a表才是 主表。
-烟花雨季 2014-07-11
  • 打赏
  • 举报
回复
这个是主表SpecialTreatmentTb 另外两个是从表,其余两个表都跟SpecialTreatmentTb 这个有关系么
t101lian 2014-07-11
  • 打赏
  • 举报
回复
引用 3 楼 u010349035 的回复:
var lowNums =
            (from a in SpecialTreatmentTb
             join b in UserTb on a.userid equals b.userid
             into c
             from d in c.DefaultIfEmpty()
             join x in GoodsTb on a.id equals x.goodid
             into y
             from z in y.DefaultIfEmpty()
             select new
             {
                 d.NickName,
                 z.Class_name,
                 a.xx//(SpecialTreatmentTb以下查这个表里面的数据)
             });
改了还是不行, 大神, 你这个是不是把 SpecialTreatmentTb 表当作 左表了? 我需要把其他两个表作为 左表
select a.* ,c.Class_name from 
( 
select a.NickName,b.*  from UserTb a 
 left join SpecialTreatmentTb  b  on a.userid=b.userid 
) a
 
left join  GoodsTb c on a.goodid=c.id
-烟花雨季 2014-07-11
  • 打赏
  • 举报
回复
ClassName = z.Class_Name == null ? "" : z.Class_Name,
                FeeRate = a.FeeRate == null ? 0 : a.FeeRate,
                BailRate = a.BailRate == null ? 0 : a.BailRate,
                SellBailRate = a.SellBailRate == null ? 0 : a.SellBailRate,
                SellFeeRate = a.SellFeeRate == null ? 0 : a.SellFeeRate,
t101lian 2014-07-11
  • 打赏
  • 举报
回复
引用 5 楼 u010349035 的回复:
[quote=引用 4 楼 t101lian 的回复:] [quote=引用 3 楼 u010349035 的回复:]
var lowNums =
            (from a in SpecialTreatmentTb
             join b in UserTb on a.userid equals b.userid
             into c
             from d in c.DefaultIfEmpty()
             join x in GoodsTb on a.id equals x.goodid
             into y
             from z in y.DefaultIfEmpty()
             select new
             {
                 d.NickName,
                 z.Class_name,
                 a.xx//(SpecialTreatmentTb以下查这个表里面的数据)
             });
引用 2 楼 caozhy 的回复:
写法和sql类似,先前两个连接,再连接第三个即可。
语法没错误, 不过查不到数据, 调试发现 存在null , 是怎么回事? [/quote] 表里面有数据为null的判断一下[/quote]
  var lowNums =
                from a in SpecialTreatmentInfoLsit
                join b in uInfoList on a.UserId equals b.UserId 
                into c 
                from d in c.DefaultIfEmpty()

                join x in GinfoList on a.GoodId equals x.ID.ToString()
                into y
                from z in y.DefaultIfEmpty()

            select new
            {
                d.UserId,
                d.NickName,
                d.Address,
                ClassName =z==null?"": z.Class_Name,
                FeeRate = a == null ? 0 : a.FeeRate,
                BailRate = a == null ? 0 : a.BailRate,
                SellBailRate = a == null ? 0 : a.SellBailRate,
                SellFeeRate = a == null ? 0 : a.SellFeeRate,
            };
都判断了, 不过调试还是提示 未引用对象实例化。。。
-烟花雨季 2014-07-11
  • 打赏
  • 举报
回复
引用 4 楼 t101lian 的回复:
[quote=引用 3 楼 u010349035 的回复:]
var lowNums =
            (from a in SpecialTreatmentTb
             join b in UserTb on a.userid equals b.userid
             into c
             from d in c.DefaultIfEmpty()
             join x in GoodsTb on a.id equals x.goodid
             into y
             from z in y.DefaultIfEmpty()
             select new
             {
                 d.NickName,
                 z.Class_name,
                 a.xx//(SpecialTreatmentTb以下查这个表里面的数据)
             });
引用 2 楼 caozhy 的回复:
写法和sql类似,先前两个连接,再连接第三个即可。
语法没错误, 不过查不到数据, 调试发现 存在null , 是怎么回事? [/quote] 表里面有数据为null的判断一下
t101lian 2014-07-11
  • 打赏
  • 举报
回复
引用 3 楼 u010349035 的回复:
var lowNums =
            (from a in SpecialTreatmentTb
             join b in UserTb on a.userid equals b.userid
             into c
             from d in c.DefaultIfEmpty()
             join x in GoodsTb on a.id equals x.goodid
             into y
             from z in y.DefaultIfEmpty()
             select new
             {
                 d.NickName,
                 z.Class_name,
                 a.xx//(SpecialTreatmentTb以下查这个表里面的数据)
             });
引用 2 楼 caozhy 的回复:
写法和sql类似,先前两个连接,再连接第三个即可。
语法没错误, 不过查不到数据, 调试发现 存在null , 是怎么回事?
-烟花雨季 2014-07-11
  • 打赏
  • 举报
回复
var lowNums =
            (from a in SpecialTreatmentTb
             join b in UserTb on a.userid equals b.userid
             into c
             from d in c.DefaultIfEmpty()
             join x in GoodsTb on a.id equals x.goodid
             into y
             from z in y.DefaultIfEmpty()
             select new
             {
                 d.NickName,
                 z.Class_name,
                 a.xx//(SpecialTreatmentTb以下查这个表里面的数据)
             });
threenewbee 2014-07-11
  • 打赏
  • 举报
回复
写法和sql类似,先前两个连接,再连接第三个即可。
-烟花雨季 2014-07-11
  • 打赏
  • 举报
回复
var lowNums =
            (from a in SpecialTreatmentInfoLsit
            join b in uInfoList on a.UserId equals b.UserId
            into c
            from d in c.DefaultIfEmpty()
            join x in GinfoList on a.GoodId equals x.ID
            into y
            from endStords in y.DefaultIfEmpty()
            select new
            {
                d.UserId,
                d.NickName,
                d.Address,
                FeeRate = endStords == null ? 0 : endStords.FeeRate,
                BailRate = endStords == null ? 0 : endStords.BailRate,
                SellBailRate = endStords == null ? 0 : endStords.SellBailRate,
                SellFeeRate = endStords == null ? 0 : endStords.SellFeeRate
            });
LINQ to SQL语句(1)之Where 2 Where操作 2 1.简单形式: 2 2.关系条件形式: 2 3.First()形式: 3 LINQ to SQL语句(2)之Select/Distinct 3 1.简单用法: 4 2.匿名类型 形式: 4 3.条件形式: 5 4.指定类 型形式: 6 5.筛选形式: 6 6.shaped形式(整形类型): 6 7.嵌套类型形式: 7 8.本地方法调用 形式(LocalMethodCall): 7 9.Distinct形式: 8 LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg 9 1.简单形式: 9 2.带条件形 式: 9 1.简单形式: 10 2.映射形式: 10 3.元素 : 11 1.简单形式: 11 2.映射形式: 11 3.元素: 11 1.简单形式: 12 2.映射形式: 12 3.元素: 12 LINQ to SQL语句(4)之Join 13 Join操作符 13 1.一对多关系(1 to Many): 13 2.多对多关系(Many to Many): 14 3.自联接关系: 15 1.双向联接(Two way join): 15 2.三向联接(There way join): 16 3.左外部联接(Left Outer Join): 17 4.投影的Let赋值(Projected let assignment): 17 5.组合键(Composite Key): 18 6.可为null/不可为null的键关系 (Nullable/Nonnullable Key Relationship): 19 LINQ to SQL语句(5)之Order By 19 Order By操作 19 1.简单形式 19 2.带条件形式 20 3.降序排序 20 4.ThenBy 20 5.ThenByDescending 22 6. 带GroupBy形式 22 LINQ to SQL语句(6)之Group By/Having 23 Group By/Having操作符 23 1.简单形式: 23 2.Select匿名类 : 24 3.最大 值 25 4.最小 值 26 5.平均 值 26 6.求和 26 7.计数 27 8.带条件计数 27 9.Where限制 28 10.多列(Multiple Columns) 28 11.表达式(Expression) 29 LINQ to SQL语句(7)之Exists/In/Any/All/Contains 29 Exists/In/Any/All/Contains操作符 29 Any 29 1.简单形式: 29 2.带条件形式: 30 All 30 Contains 31 1.包含一个对象: 31 2.包含多个值: 32 LINQ to SQL语句(8)之Concat/Union/Intersect/Except 32 Concat/Union/Intersect/Except操作 32 Concat(连接) 32 1.简单形式: 33 2.复 合形式: 33 Union(合并) 33 Intersect(相交) 34 Except(与非) 34 LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods 35 Top/Bottom操作 35 Take 35 Skip 35 TakeWhile 36 SkipWhile 36 Paging(分页)操作 36 1.索引 36 2.按唯一键排序 36 SqlMethods操作 37 Like 37 已编译查 询操作(Compiled Query) 38 LINQ to SQL语句(10)之Insert 38 插入(Insert)1.简单形式 38 2.一对多 关系 39 3.多对多关系 39 4.使用动态CUD重(Override using Dynamic CUD) 40 LINQ to SQL语句(11)之Update 41 更新(Update) 41 1.简单形式 41 2.多项更改 41 LINQ to SQL语句(12)之Delete和使用Attach 42 删除(Delete)1.简单形式 42 2.一对多关系 42 3.推理删除(Inferred Delete) 43 使用Attach更新(Update with Attach) 43 LINQ to SQL语句(13)之开放式并发控制和事务 46 Simultaneous Changes开放式并发控制 46 开放式并发(Optimistic Concurrency) 46 1.Implicit(隐式) 48 2.Explicit(显式) 48 LINQ to SQL语句(14)之Null语义和DateTime 49 Null语义 49 1.Null 49 2.Nullable.HasValue 50 日期函数 50 1.DateTime.Year 51 2.DateTime.Month 51 3.DateTime.Day 51 LINQ to SQL语句(15)之String 51 字符串(String) 51 1.字符 串串联(String Concatenation) 52 2.String.Length 52 3.String.Contains(substring) 52 4.String.IndexOf(substring) 52 5.String.StartsWith (prefix) 53 6.String.EndsWith(suffix) 53 7.String.Substring(start) 53 8.String.Substring (start, length) 53 9.String.ToUpper() 54 10.String.ToLower() 54 11.String.Trim() 54 12.String.Insert(pos, str) 54 13.String.Remove(start) 55 14.String.Remove(start, length) 55 15.String.Replace(find, replace) 55 LINQ to SQL语句(16)之对象标识 56 对象标识 56 对象缓存 56 LINQ to SQL语句(17)之对象加载 57 对象加载延迟加载 57 预先加载:LoadWith 方法 58 LINQ to SQL语句(18)之运算符转换 59 1.AsEnumerable:将类型转换为泛型 IEnumerable 59 2.ToArray:将序列转换为数组 59 3.ToList:将序列转换为 泛型列表 59 4.ToDictionary:将序 列转化为字典 60 LINQ to SQL语句(19)之ADO.NET与LINQ to SQL 60 1.连接 61 2.事务 61 LINQ to SQL语句(20)之存储过程 63 1.标量返回 63 2.单一结 果集 64 3.多个可 能形状的单一结果集 65 4.多个结果集 70 5.带输出参数 79 LINQ to SQL语句(21)之用户定义函数 80 1.在Select中使用用户定义的标量函数 80 2.在Where从句中 使用用户定义的标量函数 81 3.使用用户定义的表值函数 83 4.以联接方式使用用户定义的表值函数 84 LINQ to SQL语句(22)之DataContext 85 创建和删除数据库 85 数据库验证 88 数据库更改 88 动态查询 89 日志 90 LINQ to SQL语句(23)之动态查询 90 1.Select 91 2.Where 92 LINQ to SQL语句(24)之视图 94 LINQ to SQL语句(25)之继承 96 1.一般形式 97 2.OfType形式 98 3.IS形式 98 4.AS形式 99 5.Cast形式 99 6.UseAsDefault形式 100 7.插入新的记录 101

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧