Entity Framework 的多条件组合查询和 LIKE 查询(Combination search and simulate LIKE search with Entity Framework
来源:互联网 发布:淘宝卖家版怎么注册 编辑:程序博客网 时间:2024/06/07 12:27
我用 Visual Studio 2010 Beta 1 和 Entity Framework 4.0 写了一个例子,用于测试多条件的组合查询和 LIKE 查询。这个例子也可用于 Visual Studio 2008 SP1 和 Entity Framework 3.5。甚至两个版本的 Visual Studio 和 Entity Framework 生成的 SQL 语句也是一样的。
I wrote an example with Visual Studio 2010 Beta 1 to test the combination search and simulate LIKE search with Entity Framework 4.0. The example can also work with Visual Studio 2008 SP1 and Entity Framework 3.5. Even the generated SQL statements are equal between two versions of Visual Studio and Entity Framework.
// CREATE TABLE Manufacture(
// ManufactureID uniqueidentifier NOT NULL,
// ManufactureName nvarchar(50) NOT NULL,
// CONSTRAINT [PK_Manufacture] PRIMARY KEY CLUSTERED
// (
// ManufactureID ASC
// )
// CREATE TABLE Vehicle(
// VehicleID uniqueidentifier NOT NULL,
// VehicleName nvarchar(50) NOT NULL,
// ManufactureID uniqueidentifier NOT NULL,
// CONSTRAINT [PK_Vehicle] PRIMARY KEY CLUSTERED
// (
// [VehicleID] ASC
// )
// GO
// ALTER TABLE Vehicle WITH CHECK ADD CONSTRAINT FK_Vehicle_Manufacture FOREIGN KEY(ManufactureID)
// REFERENCES Manufacture(ManufactureID)
// GO
// ALTER TABLE Vehicle CHECK CONSTRAINT FK_Vehicle_Manufacture
// GO
static class Program
{
static void Main(string[] args)
{
try
{
do
{
Console.Write("Manufacture Name: ");
string manufactureName = Console.ReadLine();
Console.Write("Vehicle Name: ");
string vehicleName = Console.ReadLine();
using (LearningEntities db = new LearningEntities())
{
var query = db.Vehicles.Select(v => v);
if (!string.IsNullOrEmpty(manufactureName))
{
query = query.Join(
db.Manufactures.Where(
m => m.ManufactureName.Contains(manufactureName)),
v => v.Manufacture,
m => m,
(v, m) => v);
}
if (!string.IsNullOrEmpty(vehicleName))
{
query = query.Where(
v => v.VehicleName.Contains(vehicleName));
}
foreach (var v in query)
{
Console.WriteLine("Vehicle: {0}", v.VehicleName);
}
}
} while (new Func<bool>(() =>
{
Console.Write("Do you want to continue (Y | N)?");
bool result = Console.ReadKey().Key == ConsoleKey.Y;
Console.WriteLine();
return result;
})());
}
catch (Exception ex)
{
Console.WriteLine("Type: {1}{0}Message: {2}", Environment.NewLine, ex.GetType(), ex.Message);
}
Console.Write("Press any key to exit");
Console.ReadKey(true);
}
}
- Entity Framework 的多条件组合查询和 LIKE 查询(Combination search and simulate LIKE search with Entity Framework
- Entity framework lambda/ linq like 模糊查询
- Entity Framework Core Like 查询揭秘
- mvc entity framework 多条件 查询
- mvc entity framework 多条件 查询
- Entity Framework多视图条件查询、排序
- Entity framework多表查询
- Entity Framework多表多条件动态查询
- Entity Framework 动态查询
- entity framework 内联查询
- Entity Framework 简单查询
- Entity Framework 查询
- Entity framework查询原理
- Entity Framework基于方法的查询语法
- 整理一下Entity Framework的查询
- 整理一下Entity Framework的查询
- Entity Framework的查询与操作
- Entity Framework的查询与操作
- 三星U-Boot-1.1.6源码分析
- pthread_create如何传递多个参数
- ssh搭建及其配置
- vb.net DataGrid Windows 控件执行分页
- Android之Adapter用法总结
- Entity Framework 的多条件组合查询和 LIKE 查询(Combination search and simulate LIKE search with Entity Framework
- 如何将 ADO.NET Framework 整合进 Spring.NET 中 (How to integrate ADO.NET Entity Framework into Spring.NET)
- 驱动测试指导(上)
- 提问
- 读《C专家编程》笔记1
- 内存对齐
- 难死人不偿命!苹果8大笔试题及答案
- 基于S5PV210的android系统移植日记。
- 114号和116号元素命名最终确定