代码改变世界

Entity SQL rules for Wrapped and Unwrapped Results

2014-01-12 15:57  ARMdong  阅读(191)  评论(0编辑  收藏  举报

Here are some rules to remember for Entity SQL queries:

1.Use SELECT VALUE when projecting more than one type;

2.When querying with SELECT,the ObjectQuery type must be a DbDataRecord ;

3.You can use SELECT VALUE when projecting a single value or entity;

4.When querying with SELECT VALUE,the ObjectQuery type must be the same type as the value being returned.

 

用 Entity SQL 查询“包装”和“未包装” 结果的规则:

下面是几条用Entity SQL 查询需要记住的规则:

1、当投射的类型超过一个时,用 select value ;

2、当用 select 查询时,ObjectQuery 结果集类型必须为DbDataRecord类型;

3、当查询单个值或单个实体的时候,推荐使用 select value 查询;

4、当用 select value 进行查询时,ObjectQuery 类型必须和返回值类型保持一致;

 

Examples:

1、Wrapped Contact needs to be cast:

private static void WrappedContactInEntitySql()
        {
            using (var context = new PEF())
            {
                var queryString =
                    "select c from PEF.Contacts as c where c.FirstName ='Robert' ";

                var contacts = context.CreateQuery<DbDataRecord>(queryString);

                foreach (DbDataRecord c in contacts)
                {
                    var contact = (Contact)c[0];

                    Console.WriteLine("{0} {1} {2}",
                        contact.Title.Trim(),
                        contact.FirstName.Trim(),
                        contact.LastName.Trim());
                }

            }

            Console.WriteLine("Press Enter...");
            Console.ReadLine();

        }

2、Unwrapped Contact does not need to be cast:

private static void UnwrappedContactInEntitySql()
        {
            using (var context = new PEF())
            {
                var queryString =
                    "select value c from PEF.Contacts as c where c.FirstName = 'Robert' ";

                var contacts = new ObjectQuery<Contact>(queryString, context);

                foreach (var contact in contacts)
                {
                    Console.WriteLine("{0} {1} {2}",
                        contact.Title.Trim(),
                        contact.FirstName.Trim(),
                        contact.LastName.Trim());
                }

            }

            Console.WriteLine("Press Enter...");
            Console.ReadLine();

        }