diff --git a/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs b/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs index 5e32868e79..a3af4f77c6 100644 --- a/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs +++ b/src/Microsoft.OData.Client/ALinq/ExpressionWriter.cs @@ -578,12 +578,8 @@ internal override Expression VisitConstant(ConstantExpression c) listExpr.Append(uriLiteral); } - // Contains cannot be used with an empty static collection - if (listExpr.Length == 0) - { - throw new InvalidOperationException(SRResources.ALinq_ContainsNotValidOnEmptyCollection); - } - + // An empty collection is translated to the empty 'in' collection literal, e.g. "Name in ()", + // which the OData query parser supports and evaluates to no matches. listExpr.Insert(0, UriHelper.LEFTPAREN); listExpr.Append(UriHelper.RIGHTPAREN); diff --git a/src/Microsoft.OData.Client/SRResources.Designer.cs b/src/Microsoft.OData.Client/SRResources.Designer.cs index 341a89f65a..69a9585bab 100644 --- a/src/Microsoft.OData.Client/SRResources.Designer.cs +++ b/src/Microsoft.OData.Client/SRResources.Designer.cs @@ -285,15 +285,6 @@ internal static string ALinq_ConstantNotSupported { } } - /// - /// Looks up a localized string similar to The Contains method cannot be used with an empty collection.. - /// - internal static string ALinq_ContainsNotValidOnEmptyCollection { - get { - return ResourceManager.GetString("ALinq_ContainsNotValidOnEmptyCollection", resourceCulture); - } - } - /// /// Looks up a localized string similar to Could not convert constant {0} expression to string.. /// diff --git a/src/Microsoft.OData.Client/SRResources.resx b/src/Microsoft.OData.Client/SRResources.resx index 173b9a831b..c686a77a0c 100644 --- a/src/Microsoft.OData.Client/SRResources.resx +++ b/src/Microsoft.OData.Client/SRResources.resx @@ -665,9 +665,6 @@ Found an illegal type token '{0}' without a trailing navigation property. - - The Contains method cannot be used with an empty collection. - The aggregation method '{0}' is not supported. diff --git a/test/UnitTests/Microsoft.OData.Client.Tests/DataServiceQueryProviderTests.cs b/test/UnitTests/Microsoft.OData.Client.Tests/DataServiceQueryProviderTests.cs index 2d84c1a8e5..8fcdb88be8 100644 --- a/test/UnitTests/Microsoft.OData.Client.Tests/DataServiceQueryProviderTests.cs +++ b/test/UnitTests/Microsoft.OData.Client.Tests/DataServiceQueryProviderTests.cs @@ -94,7 +94,7 @@ public void TranslatesEnumerableContainsWithSpecialCharactersToInOperator() } [Fact] - public void ThrowsForEnumerableContainsWithEmptyCollection() + public void TranslatesEnumerableContainsWithEmptyCollectionToEmptyInOperator() { // Arrange var sut = new DataServiceQueryProvider(dsc); @@ -102,14 +102,14 @@ public void ThrowsForEnumerableContainsWithEmptyCollection() .Where(product => Enumerable.Empty().Contains(product.Name)); // Act - var exception = Assert.ThrowsAny(() => sut.Translate(products.Expression)); + var queryComponents = sut.Translate(products.Expression); // Assert - Assert.Equal(SRResources.ALinq_ContainsNotValidOnEmptyCollection, exception.Message); + Assert.Equal(@"http://root/Products?$filter=Name in ()", queryComponents.Uri.ToString()); } [Fact] - public void ThrowsForEnumerableContainsWithEmptyEnumCollection() + public void TranslatesEnumerableContainsWithEmptyEnumCollectionToEmptyInOperator() { // Arrange var sut = new DataServiceQueryProvider(dsc); @@ -117,10 +117,10 @@ public void ThrowsForEnumerableContainsWithEmptyEnumCollection() .Where(product => Enumerable.Empty().Contains(product.Color)); // Act - var exception = Assert.ThrowsAny(() => sut.Translate(products.Expression)); + var queryComponents = sut.Translate(products.Expression); // Assert - Assert.Equal(SRResources.ALinq_ContainsNotValidOnEmptyCollection, exception.Message); + Assert.Equal(@"http://root/Products?$filter=Color in ()", queryComponents.Uri.ToString()); } [Fact] diff --git a/test/UnitTests/Microsoft.OData.Client.Tests/Resources/SRResourcesTests.cs b/test/UnitTests/Microsoft.OData.Client.Tests/Resources/SRResourcesTests.cs index 5e926373a8..4574c21339 100644 --- a/test/UnitTests/Microsoft.OData.Client.Tests/Resources/SRResourcesTests.cs +++ b/test/UnitTests/Microsoft.OData.Client.Tests/Resources/SRResourcesTests.cs @@ -39,7 +39,6 @@ public class SRResourcesTests [InlineData("ALinq_CollectionPropertyNotSupportedInWhere", new object[] { "Value" })] [InlineData("ALinq_ConditionalNotSupported", new object[] { })] [InlineData("ALinq_ConstantNotSupported", new object[] { "Value" })] - [InlineData("ALinq_ContainsNotValidOnEmptyCollection", new object[] { })] [InlineData("ALinq_CouldNotConvert", new object[] { "Value" })] [InlineData("ALinq_ExpressionCannotEndWithTypeAs", new object[] { "Value", "Value" })] [InlineData("ALinq_ExpressionNotSupportedInProjection", new object[] { "Value", "Value" })]