Reputation: 583
I am trying to remove the duplicates from a list I binding to a gridview. However no matter what I do, the duplicates remain.
The variable lol, were added for testing the Distinct, but it didn't work. Current code:
GWportalDataContext db = new GWportalDataContext();
DeliveryTimeRepository dltRep = new DeliveryTimeRepository();
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("|", "")
};
var query2 = from o in db.AxSales
join y in db.AxSaleLines on o.SalesId equals y.SalesId
join x in db.Products on y.ItemNumber equals x.ItemNumber
where o.AccountNumber == AppSession.CurrentLoginTicket.AccountNumber
select new
{
OrderID = o.SalesId,
AxaptaSalesId = o.SalesId,
y.ItemNumber,
x.Name,
x.ProductFormatName,
y.Quantity,
Price = y.SalesPrice,
Status = dltRep.getAxOrderStatus(o.SalesId, y.ItemNumber).Substring(0, dltRep.getAxOrderStatus(o.SalesId, y.ItemNumber).LastIndexOf("|")),
Levering = dltRep.getAxOrderStatus(o.SalesId, y.ItemNumber).Substring(dltRep.getAxOrderStatus(o.SalesId, y.ItemNumber).LastIndexOf("|")).Replace("|", "")
};
//Query 1 start
var dataToList = query.ToList();
List<object> deletedItems = dataToList.Where(item => orderDeleted(item.OrderID, item.AxaptaSalesId)).Cast<object>().ToList();
var datatoGrid = dataToList.Except(deletedItems);
// Query 1 end
//Query 2 start
var dataToList2 = query2.ToList();
List<object> deletedItems2 = dataToList2.Where(item => axOrderDeleted(item.AxaptaSalesId)).Cast<object>().ToList();
var dataToGrid2 = dataToList2.Except(deletedItems2);
//Query 2 end
var combined = datatoGrid.Union(dataToGrid2);
var lol = combined.Distinct();
e.Result = lol;
Upvotes: 0
Views: 1339
Reputation: 7514
I see that you are using the .Cast<>()
extension method to cast the resulting items to object
which indicates to me that you are having a problem reconciling the two anonymous types. Assuming that the results of query1
and query2
are anonymous types with the same property names and types, this should not be the case.
Anonymous types provide overridden Equals()
and GetHashCode()
methods which are defined in terms of the Equals()
and GetHashCode()
implementations of the properties. Therefore the .Distinct()
operator should work for two anonymous type objects of the same type without requiring the IEqualityComparer implementation.
Try dropping the .Cast<object>()
extension methods from your queries. The .Union()
operation you are subsequently performing will only be allowable if the two queries are of the same data types. If that's the case, your .Distinct()
method should work just fine.
Hope this helps.
Upvotes: 1
Reputation: 15242
You need to tell Distinct what to filter on, using an IEqualityComparer
it doesn't have a way of determining what is distinct currently.
Upvotes: 2