2个linq查询同一个gridview
本文关键字:gridview 同一个 查询 linq 2个 | 更新日期: 2023-09-27 18:15:31
我想"组合" linq查询到相同的gridview。虽然我不认为我可以使用这个innerjoin。查询从数据库中获取订单,我有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
我希望这是可能的,有人可以帮助我:)
EDIT: new "problem"
我不想让变量createdDate成为第一个元素x.CreatedDate在第一个linq序列。或者第二个。我该怎么做呢?
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;
使用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);
}
}
我相信您正在寻找联合公司
试试这个链接