What is the difference between using "equals" vs == in LINQ to Entities?

Asked

Viewed 181 times

2

As quoted above, when I say equals from LINQ to Entities I am referring specifically to the reserved word of C# and not to the Equals() of System.Object.

Source: equals (Reference of C#)

The following example returns all red products.

String color = "Red";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    var query =
        from product in context.Products
        where product.Color == color
        select new
        {
            Name = product.Name,
            ProductNumber = product.ProductNumber,
            ListPrice = product.ListPrice
        };

    foreach (var product in query)
    {
        Console.WriteLine("Name: {0}", product.Name);
        Console.WriteLine("Product number: {0}", product.ProductNumber);
        Console.WriteLine("List price: ${0}", product.ListPrice);
        Console.WriteLine("");
    }
}

Source: Query expression syntax examples: filtering | Microsoft Docs

Doubt: What is the difference or impact when changing == for equals ? By making this change the select is mounted in the same way?

Obs: I don’t mean the method .Equals()

1 answer

2


You can’t use it equals, would fail because the equals is a clause of join that you’re not using.

In a join you use to link two sequences of data using the field of one against the field of the other, so it is clear that this is it and not a comparison of pure and simple equality that would return a boolean as a result, it is a different mechanism.

  • Thank you for the @Maniero reply, I was wondering that the equals could actually always be used to create a query in Brazilian, but there was no attack on the fact that it is specifically used next to the Join clause.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.