I am trying to use Linq Union to add additional record into result but Union do not work. Maybe someone could point me in right direction.
public class ProductView
{
public int Id { get; set; }
public bool Active { get; set; }
public string Name { get; set; }
public int ProductTypeId { get; set; }
public int UserCount { get; set; }
}
void Main()
{
var product = Products.Select(p => new ProductView
{
Id = p.Id,
Active = p.Active,
Name = p.Name,
ProductTypeId = p.ProductTypeId,
UserCount = 1
}).ToList();
//The new item is not jointed to the result above
product.Union(new[] {
new ProductView
{
Id = 9999,
Active = true,
Name = "Test",
ProductTypeId=0,
}
});
product.Dump();
}
You need to store the output:
In addition to this, overriding the Equals behaviour would be useful – as you probably want to check equality using just the Id field?
For example, if you don’t override the Equals behaviour, then you will get Object reference equals like this:
produces six items!
But if you change Foo to:
then you will get 3 items – which is probably what you are expecting.