• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
周巍
学习生活,学习技术,也学习英语
博客园    首页    新随笔    联系   管理    订阅  订阅

Entity Framework 的多条件组合查询和 LIKE 查询(Combination search and simulate LIKE search with Entity Framework)

我用 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.

Code
// 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);
    }
}

 

posted @ 2009-07-05 16:34  周巍  阅读(5351)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3