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.


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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值