您的位置:首页 > 其它

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

2012-06-19 08:42 363 查看



我用 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);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐