linq查询问题

【Help】 2015-04-30 01:39:33

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] array = { "111","444" };
List<Test> lstTest = new List<Test>()
{
new Test{ Name="a1",Tag="111,222,333"},
new Test{ Name="a2", Tag="222,333,444"}
};
// var lst = lstTest.Where(x => array.Contains(x.Tag)).ToList();
var lst = lstTest.Where(x => x.Tag.Contains(string.Join(",", array))).ToList();
foreach (var i in lst)
{
Console.Write("Name:{0},Tag:{1}", i.Name, i.Tag);
}
}
}

class Test
{
public string Name { get; set; }
public string Tag { get; set; }
}
}


这里的Test我是模拟数据库的数据,我想实现array数组中的字符串在Tag里面存在的数据将他查询出来,我这里写的Contains是不行的
...全文
162 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
【Help】 2015-04-30
  • 打赏
  • 举报
回复
引用 7 楼 starfd 的回复:
xxxxoooo就是你在外部循环数组拼接CHARINDEX
CHARINDEX('12','123456')>0
写反了,看到了,多谢
【Help】 2015-04-30
  • 打赏
  • 举报
回复
引用 7 楼 starfd 的回复:
xxxxoooo就是你在外部循环数组拼接CHARINDEX
CHARINDEX('12','123456')>0
这样好像写的不对 select * from Test a where exists(select 1 from Test where CHARINDEX(a.Tag,'111,222')>0)
  • 打赏
  • 举报
回复
xxxxoooo就是你在外部循环数组拼接CHARINDEX
CHARINDEX('12','123456')>0
  • 打赏
  • 举报
回复
sql就是where exists(select 1 1 from table where xxxxoooo)
  • 打赏
  • 举报
回复
数组可以直接用contains的啊 为啥要用逗号join成一个字符串
【Help】 2015-04-30
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
array.Any(y=>x.Contains(y))
那就不切割
大哥,如果用sql的方式应该怎么做?
  • 打赏
  • 举报
回复
array.Any(y=>x.Contains(y))
那就不切割
【Help】 2015-04-30
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
x.Tag.Split(',').Any(y=>array.Contains(y))
这种效率应该不是很高吧,因为要考虑到我的Test是我模拟的数据库表的数据
  • 打赏
  • 举报
回复
x.Tag.Split(',').Any(y=>array.Contains(y))
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
.NET Framework 4.0 常用类库参考手册 [微软官方 MSDN] MSDN Library - .NET Framework 4.0 - 命名空间 (节选版) 制作成了chm格式,方便离线状态下学习! ======================================= 原文地址:http://msdn.microsoft.com/zh-cn/library/ms229335.aspx ======================================= .NET Framework 类库 - 命名空间 (节选版) Microsoft.Win32 提供两种类型的类:处理由操作系统引发的事件的类和对系统注册表进行操作的类。 System 包含用于定义常用值和引用数据类型、事件和事件处理程序、接口、特性和处理异常的基础类和基类。其他类提供支持下列操作的服务:数据类型转换,方法参数操作,数学计算,远程和本地程序调用,应用程序环境管理以及对托管和非托管应用程序的监管。 System.Collections 包含定义各种对象集合(如列表、队列、位数组、哈希表和字典)的接口和类。 System.Data 包含组成大部分 ADO.NET 结构的类。ADO.NET 结构使您可以生成可用于有效管理来自多个数据源的数据的组件。 System.Data.Common 包含由 .NET Framework 数据提供程序共享的类。.NET Framework 数据提供程序描述用于在托管空间中访问数据源(如数据库)的类的集合。 System.Data.Linq 包含支持在 LINQ to SQL 应用程序中与关系数据库进行交互的类。 System.Data.Linq.Mapping 包含用于生成表示关系数据库的结构和内容的 LINQ to SQL 对象模型的类。 System.Data.Mapping 提供用于存储数据映射信息的类型。 System.Data.Metadata.Edm 包含一组类型,这些类型表示模型中由实体框架使用的概念,以及一组帮助应用程序使用元数据的类。 System.Data.Objects 包含可访问对象服务的核心功能的类。 System.Data.Objects.DataClasses 包含以下这些类:Entity Data Model (EDM) 中定义的类型的基类、由导航属性返回的类型的基类,以及用于定义将公共语言运行时 (CLR) 对象映射到概念模型中类型的特性的类。 System.Data.Sql 包含支持特定于 SQL Server 的功能的类。此类的 API 扩展已添加到 SQL Server 的 .NET Framework 数据提供程序 ( System.Data.SqlClient) 中。 System.Data.SqlClient 包含封装 SQL Server .NET Framework 数据提供程序的类。SQL Server .NET Framework 数据提供程序描述了用于在托管空间中访问 SQL Server 数据库的类集合。 System.IO 包含允许对数据流和文件进行同步和异步读写的类型。 System.Linq 包含支持使用语言集成查询 (LINQ) 的查询的类和接口。 System.Web 提供启用浏览器/服务器通信的类和接口。 ……

8,494

社区成员

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

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