Entity Framework 的多条件组合查询和 LIKE 查询(Combination search and simulate LIKE search with Entity Framework
2012-06-19 08:42
363 查看
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.
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
// 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
![](http://www.cnblogs.com/Images/dot.gif)
");
Console.ReadKey(true);
}
}
相关文章推荐
- Entity Framework 的多条件组合查询和 LIKE 查询(Combination search and simulate LIKE search with Entity Framework)
- SQLITE WITH ENTITY FRAMEWORK CODE FIRST AND MIGRATION
- MongoDB 组合多个条件查询($and、$in、$gte、$lte)
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
- Using the Repository Pattern with ASP.NET MVC and Entity Framework
- Repository Pattern with Entity Framework 4.1 and Code First
- EntityFramework linq 多条件查询,不定条件查询
- Elasticsearch进行and,or多条件组合DSL结构化查询
- Using Repository and Unit of Work patterns with Entity Framework 4.0
- Entity Framework Core Like 查询揭秘
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
- ASP.NET MVC Example with Northwind and Entity Framework Published
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第六章:管理产品图片——多对多关系(上篇)
- Using Repository and Unit of Work patterns with Entity Framework 4.0
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- mvc entity framework 多条件 查询
- EntityFramework.Extended 实现Linq 动态查询和动态条件
- Inner join and Left Outer Joins in LINQ with Entity Framework
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第六章:管理产品图片——多对多关系(上篇)
- mvc entity framework 多条件 查询