linq join on 多条件查询 or 连接

菜鸟6号 2014-10-11 10:04:14
select *
from tabel1
inner join table2
on tabel1.id = table2.recipegroupid
or tabel1.id = table2.recipeitemid



把上面sql语句转换为linq,注意多条件查询中用的是 or 连接
...全文
3139 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2014-10-11
  • 打赏
  • 举报
回复
昨天回答过你了。
引用 1 楼 dongxinxi 的回复:
换成where一样的 var result = from t1 in table1 from t2 in table2 where t1.id == t2.recipegroupid || t1.id == t2.recipeitemid select t1; 或者子查询
换成where结果一样,但是实现机制不同,所以性能上有很大的差距。join的复杂度是log(N),迪卡尔乘积再where过滤的复杂度是N^2。这个昨天也和lz说过了。
q107770540 2014-10-11
  • 打赏
  • 举报
回复
用where是个解决方案,但是用WHERE后就不是INNER JOIN了,而是 CROSS JOIN了
  • 打赏
  • 举报
回复
换成where一样的 var result = from t1 in table1 from t2 in table2 where t1.id == t2.recipegroupid || t1.id == t2.recipeitemid select t1; 或者子查询
threenewbee 2014-10-11
  • 打赏
  • 举报
回复
引用 5 楼 dongxinxi 的回复:
那就分别Join,将结果集Union 如果是内存对象就好办了(自己实现一个比较器),SQL查询没想到更好的办法 如果两个字段只有一个有值,那就用IsNull(table2.recipegroupid, table2.recipeitemid) Linq中用Nullable<int>.HasValue判断 这种表的设计没有遇到过,一个表的主键同时对应到另一个表的两个字段,如果用到了联合主键,表之间也应该保持对应
该说的都和他说过了,包括用union,以及直接用sql。
  • 打赏
  • 举报
回复
那就分别Join,将结果集Union 如果是内存对象就好办了(自己实现一个比较器),SQL查询没想到更好的办法 如果两个字段只有一个有值,那就用IsNull(table2.recipegroupid, table2.recipeitemid) Linq中用Nullable<int>.HasValue判断 这种表的设计没有遇到过,一个表的主键同时对应到另一个表的两个字段,如果用到了联合主键,表之间也应该保持对应
菜鸟6号 2014-10-11
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
昨天回答过你了。 [quote=引用 1 楼 dongxinxi 的回复:] 换成where一样的 var result = from t1 in table1 from t2 in table2 where t1.id == t2.recipegroupid || t1.id == t2.recipeitemid select t1; 或者子查询
换成where结果一样,但是实现机制不同,所以性能上有很大的差距。join的复杂度是log(N),迪卡尔乘积再where过滤的复杂度是N^2。这个昨天也和lz说过了。[/quote] 表1和表2的数据都比较大,可能过千万条数据,用where性能不是会很底
The professional programmer’s Deitel® guide to C# 6 and object-oriented development for Windows® Written for programmers with a background in high-level language programming, C# 6 for Programmers applies the Deitel signature live-code approach to teaching programming and explores Microsoft’s C# 6 and .NET in depth. Concepts are presented in the context of 170+ fully coded and tested apps, complete with syntax shading, code highlighting, code walkthroughs, program outputs and hundreds of savvy software-development tips. Start with an introduction to C# using an early classes and objects approach, then rapidly move on to more advanced topics, including LINQ, asynchronous programming with async and await and more. You’ll enjoy the treatment of object-oriented programming and an object-oriented design/UML® ATM case study, including a complete C# implementation. When you’ve mastered the book, you’ll be ready to start building industrial-strength, object-oriented C# apps. Paul Deitel and Harvey Deitel are the founders of Deitel & Associates, Inc., the internationally recognized programming languages authoring and corporate training organization. Millions of people worldwide have used Deitel textbooks, professional books, LiveLessons™ video products, e-books, resource centers and REVEL™ interactive multimedia courses with integrated labs and assessment to master major programming languages and platforms, including C#, C++, C, Java™, Android™ app development, iOS app development, Swift™, Visual Basic®, Python™ and Internet and web programming. Features: Use with Windows® 7, 8 or 10. Integrated coverage of new C# 6 functionality: string interpolation, expression-bodied methods and properties, auto-implemented property initializers, getter-only properties, nameof, null-conditional operator, exception filters and more. Entertaining and challenging code examples. Deep treatment of classes, objects, inheritance, polymorphism and interfaces. Generics, LINQ and generic collections; PLINQ (Parallel LINQ) for multicore performance. Asynchronous programming with async and await; functional programming with lambdas, delegates and immutability. Files; relational database with LINQ to Entities. Object-oriented design ATM case study with full code implementation. Emphasis on performance and software engineering principles. Visit www.deitel.com Download code examples For Deitel’s programming training courses, www.deitel.com/training or write to deitel@deitel.com Join the Deitel social networking communities: Facebook® at facebook.com/DeitelFan Twitter® at twitter.com/deitel LinkedIn® at bit.ly/DeitelLinkedIn YouTube™ at youtube.com/DeitelTV Google+™ at google.com/+DeitelFan Subscribe to the Deitel® Buzz e-mail newsletter at www.deitel.com/newsletter/subscribe.html Table of Contents Chapter 1 Introduction Chapter 2 Introduction to Visual Studio and Visual Programming Chapter 3 Introduction to C# App Programming Chapter 4 Introduction to Classes, Objects, Methods and strings Chapter 5 Control Statements: Part 1 Chapter 6 Control Statements: Part 2 Chapter 7 Methods: A Deeper Look Chapter 8 Arrays; Introduction to Exception Handling Chapter 9 Introduction to LINQ and the List Collection Chapter 10 Classes and Objects: A Deeper Look Chapter 11 Object-Oriented Programming: Inheritance Chapter 12 OOP: Polymorphism and Interfaces Chapter 13 Exception Handling: A Deeper Look Chapter 14 Graphical User Interfaces with Windows Forms: Part 1 Chapter 15 Graphical User Interfaces with Windows Forms: Part 2 Chapter 16 Strings and Characters: A Deeper Look Chapter 17 Files and Streams Chapter 18 Generics Chapter 19 Generic Collections; Functional Programming with LINQ/PLINQ Chapter 20 Databases and LINQ Chapter 21 Asynchronous Programming with async and await Appendix A: Operator Precedence Chart Appendix B: Simple Types Appendix C: ASCII Character Set

8,493

社区成员

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

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