zoukankan      html  css  js  c++  java
  • LINQ to Entities,基于方法查询,查询表达式语法

    初次学习LINQ to Entities,给出两种查询的示例代码。

    基于方法的查询:

    using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
    {
        ObjectSet<Contact> contacts = AWEntities.Contacts;
        ObjectSet<SalesOrderHeader> orders = AWEntities.SalesOrderHeaders;

        var query =
        contacts.SelectMany(
            contact => orders.Where(order =>
                (contact.ContactID == order.Contact.ContactID)
                    && order.OrderDate >= new DateTime(2002, 10, 1))
                .Select(order => new
                {
                    ContactID = contact.ContactID,
                    LastName = contact.LastName,
                    FirstName = contact.FirstName,
                    OrderID = order.SalesOrderID,
                    OrderDate = order.OrderDate
                }));

        foreach (var order in query)
        {
            Console.WriteLine("Contact ID: {0} Name: {1}, {2} Order ID: {3} Order date: {4:d} ",
                order.ContactID, order.LastName, order.FirstName,
                order.OrderID, order.OrderDate);
        }
    }

    查询表达式:

    using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
    {
        ObjectSet<Contact> contacts = AWEntities.Contacts;
        ObjectSet<SalesOrderHeader> orders = AWEntities.SalesOrderHeaders;

        var query =
            from contact in contacts
            from order in orders
            let total = order.TotalDue
            where contact.ContactID == order.Contact.ContactID
                && total >= 10000.0M
            select new
            {
                ContactID = contact.ContactID,
                LastName = contact.LastName,
                OrderID = order.SalesOrderID,
                total
            };

        foreach (var order in query)
        {
            Console.WriteLine("Contact ID: {0} Last name: {1} Order ID: {2} Total: {3}",
                order.ContactID, order.LastName, order.OrderID, order.total);
        }
    }

  • 相关阅读:
    [摘抄]数据湖,大数据的下一个变革
    linux localhost 借助nginx 支持https
    mac os 下 android studio 选择模拟器设备的时候一直显示Loading
    SpringBoot 项目遇到错误: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    nexus-2.12.0-01 shiro漏洞升级
    IDEA 历史版本下载地址
    Postfix 554 5.7.1 Relay Access Denied
    Java SPI 机制实现项目框架高扩展性
    IDEA控制台 springboot输出中文乱码
    Setup Apache2 in Debian 9 and enable two ports for two sites
  • 原文地址:https://www.cnblogs.com/jazzdan/p/2006354.html
Copyright © 2011-2022 走看看