2 linq查询相同的gridview

时间:2011-11-24 10:10:12

标签: c# linq web

我想将linq查询“组合”到同一个gridview中。虽然我不认为我可以使用内连接。 查询从数据库获取订单,我有2个表来自不同地方的订单。 在gridview中,我想显示第一个表和第二个表中的所有订单。 像这样:

OrderID - Item - Amount etc ---From table 1
OrderID - Item - Amount etc ---From table 2
etc..

我目前从第一张表中获取订单的查询是:

var query = from o in db.Orders
            join y in db.OrderLines on o.OrderID equals y.OrderID
            join x in db.Products on y.ItemNumber equals x.ItemNumber
            where o.AccountNumber == AppSession.CurrentLoginTicket.AccountNumber
            select new
            {
                o.OrderID,
                o.AxaptaSalesId,
                y.ItemNumber,
                x.Name,
                x.ProductFormatName,
                y.Quantity,
                y.Price,
                Status = dltRep.getOrderStatus(o.OrderID, o.AxaptaSalesId, y.ItemNumber).Substring(0, dltRep.getOrderStatus(o.OrderID, o.AxaptaSalesId, y.ItemNumber).LastIndexOf("|")),
                Levering = dltRep.getOrderStatus(o.OrderID, o.AxaptaSalesId, y.ItemNumber).Substring(dltRep.getOrderStatus(o.OrderID, o.AxaptaSalesId, y.ItemNumber).LastIndexOf("|")).Replace("|", "")
            };

另一张表将具有相同的信息。它被命名为AxSale。

我希望这是可能的,有人可以帮助我:)

编辑:新“问题”

我不想让变量createdDate成为第一个linq seq中的第一个元素x.CreatedDate。或者第二个。 我该怎么做?

var created = purchases.OrderByDescending(x => x.CreatedDate).
                Select(x => new { x.CreatedDate, x.LineSupplierAccountNO }).
                GroupBy(x => x.LineSupplierAccountNO);

if (created.Count() > 1)
{
    created = purchases.OrderBy(x => x.CreatedDate).
                Select(x => new { x.CreatedDate, x.LineSupplierAccountNO }).
                GroupBy(x => x.LineSupplierAccountNO);
}

var createdDate = created.FirstOrDefault();

解决方案代码:

var created = purchases.OrderBy(x => x.CreatedDate).Select(x => x);

if (created.GroupBy(x => x.LineSupplierAccountNO).Count() > 1)
{
    created = purchases.OrderByDescending(x => x.CreatedDate).Select(x => x);
}

var createdDate = created.First().CreatedDate;

2 个答案:

答案 0 :(得分:1)

我相信您正在寻找Union

试试这个link

答案 1 :(得分:1)

使用UNION运算符连接具有相同字段的结果。以下是http://code.msdn.microsoft.com/windowsdesktop/101-LINQ-Samples-3fb9811b

的示例
public void Linq49() 
{ 
    List<Product> products = GetProductList(); 
    List<Customer> customers = GetCustomerList(); 

    var productFirstChars = 
        from p in products 
        select p.ProductName[0]; 
    var customerFirstChars = 
        from c in customers 
        select c.CompanyName[0]; 

    var uniqueFirstChars = productFirstChars.Union(customerFirstChars); 

    Console.WriteLine("Unique first letters from Product names and Customer names:"); 
    foreach (var ch in uniqueFirstChars) 
    { 
        Console.WriteLine(ch); 
    } 
}