SequenceEqual does not work below in comparing class
I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.
public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}
List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();
product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
c# generics asp.net-core .net-core
add a comment |
I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.
public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}
List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();
product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
c# generics asp.net-core .net-core
Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.
– jonsca
Jan 2 at 5:06
add a comment |
I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.
public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}
List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();
product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
c# generics asp.net-core .net-core
I am trying to compare the following data below. How come SequenceEqual does not work below? I created two list classes, and want to compare.
public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}
List<Product> product1 = new List<Product>();
List<Product> product2 = new List<Product>();
product1.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
product2.Add(new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
});
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
c# generics asp.net-core .net-core
c# generics asp.net-core .net-core
asked Jan 2 at 4:55
HardyWestHardyWest
419
419
Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.
– jonsca
Jan 2 at 5:06
add a comment |
Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.
– jonsca
Jan 2 at 5:06
Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.
– jonsca
Jan 2 at 5:06
Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.
– jonsca
Jan 2 at 5:06
add a comment |
2 Answers
2
active
oldest
votes
SequenceEqual
compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product
references.
If you change your code to use the same Product
in each list then SequenceEqual
will return `true'
var product = new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
};
product1.Add(p);
product2.Add(p);
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
Why? Because in this example the lists contain the same object.
Read this for more information on how SequenceEqual
compares the two sequences.
If you want SequenceEqual
to return true if the properties of the Product
class are the same then you need to override the Equals
method on the Product
class.
EDIT
As others have mentioned, it is a better practice to implement the IEqualityComparer<T>
interface instead of directly overriding the Equals
method.
add a comment |
By default, the SequenceEqual
method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.
If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):
- have the
Product
class implement theIEquatable<Product>
interface; or - create a separate class that implements the
IEqualityComparer<Product>
interface and use the overload ofSequenceEqual
that takes an instance ofIEqualityComparer<T>
I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001392%2fsequenceequal-does-not-work-below-in-comparing-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
SequenceEqual
compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product
references.
If you change your code to use the same Product
in each list then SequenceEqual
will return `true'
var product = new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
};
product1.Add(p);
product2.Add(p);
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
Why? Because in this example the lists contain the same object.
Read this for more information on how SequenceEqual
compares the two sequences.
If you want SequenceEqual
to return true if the properties of the Product
class are the same then you need to override the Equals
method on the Product
class.
EDIT
As others have mentioned, it is a better practice to implement the IEqualityComparer<T>
interface instead of directly overriding the Equals
method.
add a comment |
SequenceEqual
compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product
references.
If you change your code to use the same Product
in each list then SequenceEqual
will return `true'
var product = new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
};
product1.Add(p);
product2.Add(p);
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
Why? Because in this example the lists contain the same object.
Read this for more information on how SequenceEqual
compares the two sequences.
If you want SequenceEqual
to return true if the properties of the Product
class are the same then you need to override the Equals
method on the Product
class.
EDIT
As others have mentioned, it is a better practice to implement the IEqualityComparer<T>
interface instead of directly overriding the Equals
method.
add a comment |
SequenceEqual
compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product
references.
If you change your code to use the same Product
in each list then SequenceEqual
will return `true'
var product = new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
};
product1.Add(p);
product2.Add(p);
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
Why? Because in this example the lists contain the same object.
Read this for more information on how SequenceEqual
compares the two sequences.
If you want SequenceEqual
to return true if the properties of the Product
class are the same then you need to override the Equals
method on the Product
class.
EDIT
As others have mentioned, it is a better practice to implement the IEqualityComparer<T>
interface instead of directly overriding the Equals
method.
SequenceEqual
compares the two sequences to see if the contain the same objects. In your case they don't. They contain different Product
references.
If you change your code to use the same Product
in each list then SequenceEqual
will return `true'
var product = new Product
{
ProductId = 1,
ProductName = "TV",
ProductDescription = "Television Set"
};
product1.Add(p);
product2.Add(p);
if (product1.SequenceEqual(product2))
{
Console.WriteLine("equal data");
}
else
{
Console.WriteLine("Not equal data");
}
Why? Because in this example the lists contain the same object.
Read this for more information on how SequenceEqual
compares the two sequences.
If you want SequenceEqual
to return true if the properties of the Product
class are the same then you need to override the Equals
method on the Product
class.
EDIT
As others have mentioned, it is a better practice to implement the IEqualityComparer<T>
interface instead of directly overriding the Equals
method.
edited Jan 2 at 5:17
answered Jan 2 at 5:06
Simply GedSimply Ged
2,67621523
2,67621523
add a comment |
add a comment |
By default, the SequenceEqual
method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.
If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):
- have the
Product
class implement theIEquatable<Product>
interface; or - create a separate class that implements the
IEqualityComparer<Product>
interface and use the overload ofSequenceEqual
that takes an instance ofIEqualityComparer<T>
I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.
add a comment |
By default, the SequenceEqual
method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.
If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):
- have the
Product
class implement theIEquatable<Product>
interface; or - create a separate class that implements the
IEqualityComparer<Product>
interface and use the overload ofSequenceEqual
that takes an instance ofIEqualityComparer<T>
I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.
add a comment |
By default, the SequenceEqual
method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.
If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):
- have the
Product
class implement theIEquatable<Product>
interface; or - create a separate class that implements the
IEqualityComparer<Product>
interface and use the overload ofSequenceEqual
that takes an instance ofIEqualityComparer<T>
I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.
By default, the SequenceEqual
method compares elements in the collection using the default comparer, which does a reference comparison — See the official documentation page, especially the Examples section.
If you want to compare the data of the objects rather than the references, you have two options (that I took from the page linked above):
- have the
Product
class implement theIEquatable<Product>
interface; or - create a separate class that implements the
IEqualityComparer<Product>
interface and use the overload ofSequenceEqual
that takes an instance ofIEqualityComparer<T>
I suggest that you read the documentation page for more detailed information and examples on how to implement these two options.
answered Jan 2 at 5:07
Mickaël DerrieyMickaël Derriey
4,8822440
4,8822440
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001392%2fsequenceequal-does-not-work-below-in-comparing-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Without an equality comparer (third example down), your code is just comparing the references, which are different since they are two different objects.
– jonsca
Jan 2 at 5:06