是否有可能写出这样的东西?

时间:2014-02-20 10:15:08

标签: c# linq

我可以这样做吗?:

     public class Customer
            {
                public int cid { get; set; }
                public string FirstName { get; set; }
                public string LastName { get; set; }
                public string City { get; set; }

     public class Order
        {
            public int Orderid { get; set; }
            public int customerid { get; set; }
            public int Quantity { get; set; }
        }



     static void Main(string[] args)
        {

            List<Customer> customerList = new List<Customer>
            {
              new Customer(){cid=1,FirstName="Avinash",LastName="Kothamasu",City="Chennai"},
              new Customer(){cid=2,FirstName="Abhiram",LastName="Kumar",City="Chennai"},
          new Customer{cid=3,FirstName="Balu",LastName="Chinna",City="Chennai"},
          new Customer{cid=4,FirstName="Sai",LastName="Kothamasu",City="Bhimavaram"},
          new Customer{cid=5,FirstName="Harshita",LastName="Kothamasu",City="Bhimarvarm"},
          new Customer{cid=6,FirstName="Mounika",LastName="Kothamasu",City="Guntur"}
        };

        List<Order> orderList = new List<Order>
        {
            new Order(){Orderid=11,customerid=1,Quantity=20},
            new Order(){Orderid=111,customerid=1,Quantity=10},
            new Order(){Orderid=22,customerid=2,Quantity=15},
            new Order(){Orderid=222,customerid=2,Quantity=8},
            new Order(){Orderid=22222,customerid=2,Quantity=50},
            new Order(){Orderid=33,customerid=3,Quantity=10},
            new Order(){Orderid=333,customerid=3,Quantity=5},
            new Order(){Orderid=44,customerid=4,Quantity=4},
            new Order(){Orderid=55,customerid=5,Quantity=1},
            new Order(){Orderid=66,customerid=6,Quantity=49}
        };


        var OrderListforCustomers = (from c in customerList
                                    join o in orderList on
                                    c.cid equals o.customerid
                                    where o.Quantity >= 1 && o.Quantity <= 50
                                    group new { c, o } by new { o.customerid, c.FirstName, c.LastName, c.City } into customergroups
                                    orderby customergroups.Select(x => x.o).Max(x => x.Quantity) descending, customergroups.Key.customerid descending
                                    select new 
                                        {
                                            CustomerID = customergroups.Key.customerid,
                                            CustomerName = customergroups.Key.FirstName.ToUpper() + " , " + customergroups.Key.LastName.ToUpper(),
                                            City = customergroups.Key.City,
                                            MaximumQuantityCount = customergroups.Select(x => x.o).Max(x => x.Quantity),
                                            OrderIDWithMaxCount = customergroups.Select(x => x.o).OrderByDescending(x => x.Quantity).Select(x => x.Orderid).FirstOrDefault()
                                        } into result).Take(6); // Error here

        Console.Write("CID" + " || ");
        Console.Write("CustomerName" + " || ");
        Console.Write("City" + " | ");
        Console.Write("MaximumQuantityCount" + " || ");
        Console.Write("OrderIDwithMaxQuantityCount");
        Console.WriteLine(Environment.NewLine);

        foreach (var custgroup in limOrderListCust)
        {
            Console.Write(custgroup.CustomerID + " || ");
            Console.Write(custgroup.CustomerName + " || ");
            Console.Write(custgroup.City + " || ");
            Console.Write(custgroup.MaximumQuantityCount + " || ");
            Console.Write(custgroup.OrderIDWithMaxCount);
            Console.Write(Environment.NewLine);
        }

        Console.ReadLine();

我想从结果查询数据中仅检索前6条记录。

我可以写一些类似的东西:

var topdata=OrderListforCustomers .Take(6);

但是,我想只在单个查询中获得结果。

更新

我收到错误:

A query body must end with a select clause or a group clause    

请建议

1 个答案:

答案 0 :(得分:3)

只需删除into result子句,只需要继续。

查询表达式的语法详见http://msdn.microsoft.com/en-us/library/bb308959.aspx

以下是相关部分:

  

query-expression :: = from-clause query-body

     

query-body :: =        query-body-clause * final-query-clause query-continuation?

     

query-continuation :: = into itemName query-body

正如您所看到的,into result后面必须跟一个查询正文才能生效。