Linq 中的动态 Where 查询如何实现 OR ?

Triumph 2014-05-08 09:10:57
Info 表中有两个字段:N 和 V,都是字符串类型,现要查询:N 等于 n1 并且 V 包含 v1,或者N等于 n2 并且 V 包含 v2,或者 N 等于 n2 并且 V 包含 v3,写 linq 如下:

from t in info where (t.N = "n1" And t.V.Contains("v1")) Or (t.N = "n2" And t.V.Contains("v2")) Or (t.N = "n3" And t.V.Contains("v3"))

这个运行正确。

但实际上查询的关键字是动态的,数量是未知的,于是建立一个 Dictionary(Of String, String):

Dim keyList As New Dictionary(Of String, String)
keyList.Add("n1","v1")
keyList.Add("n2","v2")
keyList.Add("n3","v3")
....
....

dim _q = From t In info
For Each key As String In keyList.Keys
dim _v As String = keyList(key)
_q = _q.Where(Function (x) x.N = key And x.V.Contains(_v))
Next

这样最终的查询条件就全都是 And 而不是前一个的OR b ,结果当然不正确。

请教各位如何这种情况如何实现?
...全文
647 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Triumph 2014-05-10
  • 打赏
  • 举报
回复
romanchaos:后一个管用,而且思路很好,让我豁然开朗。非常感谢!!!
romanchaos 2014-05-09
  • 打赏
  • 举报
回复
修改一下 dim _q = From t In info dim _p = IQuaeryable<info> For Each key As String In keyList.Keys dim _v As String = keyList(key) _p = _p.Union(_q.Where(条件Next)) Next
romanchaos 2014-05-09
  • 打赏
  • 举报
回复
dim _q = From t In info Where 条件1 For Each key As String In keyList.Keys dim _v As String = keyList(key) _q = _q.Union(_q.Where(条件Next)) Next
Triumph 2014-05-09
  • 打赏
  • 举报
回复
结果不正确呀?连单个的都查询不出来了
threenewbee 2014-05-08
  • 打赏
  • 举报
回复
_q = _q..Union(_q.Where(Function (x) 下一个条件))
关于LINQ的一本书籍,附上源码,喜欢请购买正版。 LINQ is the part of the .NET Framework that provides a generic approach to querying data from different data sources. It has quickly become the next must-have skill for .NET developers. Pro LINQ: Language Integrated Query in C# 2010 is all about code. Literally, this book starts with code and ends with code. Most books show the simplest examples of how to use a method, but they so rarely show how to use the more complex prototypes. This book is different. Demonstrating the overwhelming majority of LINQ operators and prototypes, it is a veritable treasury of LINQ examples. Rather than obscure the relevant LINQ principles in code examples by focusing on a demonstration application you have no interest in writing, this book cuts right to the chase of each LINQ operator, method, or class. However, where complexity is necessary to truly demonstrate an issue, the examples are right there in the thick of it. For example, code samples demonstrating how to handle concurrency conflicts actually create concurrency conflicts so you can step through the code and see them unfold. Face it, most technical books, while informative, are dull. LINQ need not be dull. Written with a sense of humor, this book will attempt to entertain you on your journey through the wonderland of LINQ and C# 2010. What you’ll learn How to leverage all the new LINQ relevant C# 2008 language features including extension methods, lambda expressions, anonymous data types, and partial methods. How to use LINQ to Objects to query in-memory data collections such as arrays, ArrayLists, and Lists to retrieve the data you want. Why some queries are deferred, how a deferred query can bite you, and how you can make deferred queries work for you. How to use LINQ to XML to revolutionize your creation, manipulation, and searching of XML data. How to query DataSets with LINQ to DataSet so you can coexist with legacy code and use LINQ to query databases other than SQL Server. How to query Databases with LINQ to SQL, write your own entity classes, and understand how to handle concurrency conflicts. Who this book is for This book is written for the proficient C# developer, but you do not need to be up on all the latest C# features to understand the material. When you finish this book, you will have a much greater understanding of the latest C# features. Table of Contents Hello LINQ C# Language Enhancements for LINQ LINQ to Objects Introduction Deferred Operators Nondeferred Operators LINQ to XML Introduction The LINQ to XML API LINQ to XML Operators Additional XML Capabilities LINQ to DataSet Operators Additional DataSet Capabilities LINQ to SQL Introduct ion LINQ to SQL Tips and Tools LINQ to SQL Database Operations LINQ to SQL Ent ity Classes The LINQ to SQL DataContext LINQ to SQL Concurrency Conflicts Additional LINQ to SQL Capabilities LINQ to Entities Introduction LINQ to Entities Operations LINQ to Entities Classes Parallel LINQ Introduction Using Parallel LINQ Parallel LINQ Operators
LINQ is the part of the .NET Framework that provides a generic approach to querying data from different data sources. It has quickly become the next must-have skill for .NET developers. Pro LINQ: Language Integrated Query in C# 2010 is all about code. Literally, this book starts with code and ends with code. Most books show the simplest examples of how to use a method, but they so rarely show how to use the more complex prototypes. This book is different. Demonstrating the overwhelming majority of LINQ operators and prototypes, it is a veritable treasury of LINQ examples. Rather than obscure the relevant LINQ principles in code examples by focusing on a demonstration application you have no interest in writing, this book cuts right to the chase of each LINQ operator, method, or class. However, where complexity is necessary to truly demonstrate an issue, the examples are right there in the thick of it. For example, code samples demonstrating how to handle concurrency conflicts actually create concurrency conflicts so you can step through the code and see them unfold. Face it, most technical books, while informative, are dull. LINQ need not be dull. Written with a sense of humor, this book will attempt to entertain you on your journey through the wonderland of LINQ and C# 2010. What you’ll learn How to query Databases with LINQ to SQL, write your own entity classes, and understand how to handle concurrency conflicts. * How to leverage all the new LINQ relevant C# 2008 language features including extension methods, lambda expressions, anonymous data types, and partial methods. * How to use LINQ to Objects to query in-memory data collections such as arrays, ArrayLists, and Lists to retrieve the data you want. * Why some queries are deferred, how a deferred query can bite you, and how you can make deferred queries work for you. * How to use LINQ to XML to revolutionize your creation, manipulation, and searching of XML data. * How to query DataSets with LINQ to DataSet so you can coexist with legacy code and use LINQ to query databases other than SQL Server. Who is this book for? Apress, 2010 This book is written for the proficient C# developer, but you do not need to be up on all the latest C# features to understand the material. When you finish this book, you will have a much greater understanding of the latest C# features. amazon link:http://www.amazon.com/exec/obidos/ASIN/1430226536/buythisbooks-20

8,497

社区成员

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

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