public void LinqAttributes_Counter_AttemptInsert() { // Create config that uses linq based attributes var mappingConfig = new MappingConfiguration(); mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(CounterEntityWithLinqAttributes), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(CounterEntityWithLinqAttributes))); var table = new Table <CounterEntityWithLinqAttributes>(Session, mappingConfig); CounterEntityWithLinqAttributes pocoAndLinqAttributesLinqPocos = new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }; var expectedErrMsg = "INSERT statement(s)? are not allowed on counter tables, use UPDATE instead"; TestCluster.PrimeFluent( b => b.WhenQuery( "INSERT INTO \"CounterEntityWithLinqAttributes\" (\"Counter\", \"KeyPart1\", \"KeyPart2\") VALUES (?, ?, ?)", when => pocoAndLinqAttributesLinqPocos.WithParams(when)) .ThenServerError( ServerError.Invalid, expectedErrMsg)); var e = Assert.Throws <InvalidQueryException>(() => Session.Execute(table.Insert(pocoAndLinqAttributesLinqPocos))); Assert.AreEqual(expectedErrMsg, e.Message); }
private void PrimeLinqCounterQuery(CounterEntityWithLinqAttributes counter) { TestCluster.PrimeDelete(); TestCluster.PrimeFluent( b => b.WhenQuery( "SELECT \"Counter\", \"KeyPart1\", \"KeyPart2\" " + "FROM \"CounterEntityWithLinqAttributes\" " + "WHERE \"KeyPart1\" = ? AND \"KeyPart2\" = ?", when => counter.WithParams(when, "KeyPart1", "KeyPart2")) .ThenRowsSuccess(counter.CreateRowsResult())); }
public void LinqCounter_BatchTest() { var mappingConfig = new MappingConfiguration(); mappingConfig.Define(new Map <CounterEntityWithLinqAttributes>() .ExplicitColumns() .Column(t => t.KeyPart1) .Column(t => t.KeyPart2) .Column(t => t.Counter, map => map.AsCounter()) .PartitionKey(t => t.KeyPart1, t => t.KeyPart2) .TableName("linqcounter_batchtest_table") ); var counterTable = new Table <CounterEntityWithLinqAttributes>(_session, mappingConfig); counterTable.CreateIfNotExists(); var counter = new CounterEntityWithLinqAttributes { KeyPart1 = Guid.NewGuid(), KeyPart2 = 1, Counter = 1 }; var counter2 = new CounterEntityWithLinqAttributes { KeyPart1 = counter.KeyPart1, KeyPart2 = 2, Counter = 2 }; var batch = counterTable.GetSession().CreateBatch(BatchType.Counter); var update1 = counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 1 }) .Update(); var update2 = counterTable .Where(t => t.KeyPart1 == counter2.KeyPart1 && t.KeyPart2 == counter2.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 2 }) .Update(); batch.Append(update1); batch.Append(update2); batch.Execute(); var counters = counterTable.Execute().ToList(); Assert.AreEqual(2, counters.Count); Assert.IsTrue(counters.Contains(counter)); Assert.IsTrue(counters.Contains(counter2)); }
public void LinqAttributes_Counter_AttemptInsert() { // Create config that uses linq based attributes var mappingConfig = new MappingConfiguration(); mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(CounterEntityWithLinqAttributes), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(CounterEntityWithLinqAttributes))); var table = new Table <CounterEntityWithLinqAttributes>(_session, mappingConfig); table.Create(); CounterEntityWithLinqAttributes pocoAndLinqAttributesLinqPocos = new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }; // Validate Error Message var e = Assert.Throws <InvalidQueryException>(() => _session.Execute(table.Insert(pocoAndLinqAttributesLinqPocos))); string expectedErrMsg = "INSERT statement(s)? are not allowed on counter tables, use UPDATE instead"; StringAssert.IsMatch(expectedErrMsg, e.Message); }
public void LinqAttributes_Counter_Increments(int increment) { // Create config that uses linq based attributes var mappingConfig = new MappingConfiguration(); var counterTable = new Table <CounterEntityWithLinqAttributes>(_session, mappingConfig); counterTable.CreateIfNotExists(); var counter = new CounterEntityWithLinqAttributes { KeyPart1 = Guid.NewGuid(), KeyPart2 = 1 }; counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = increment }) .Update().Execute(); var updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2).Execute().FirstOrDefault(); Assert.AreEqual(increment, updatedCounter.Counter); counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = increment }) .Update().Execute(); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2).Execute().FirstOrDefault(); Assert.AreEqual(increment * 2, updatedCounter.Counter); counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = increment }) .Update().Execute(); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2).Execute().FirstOrDefault(); Assert.AreEqual(increment * 3, updatedCounter.Counter); //testing negative values int negativeIncrement = -1 * increment; counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = negativeIncrement }) .Update().Execute(); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2).Execute().FirstOrDefault(); Assert.AreEqual(increment * 2, updatedCounter.Counter); counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = negativeIncrement }) .Update().Execute(); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2).Execute().FirstOrDefault(); Assert.AreEqual(increment, updatedCounter.Counter); counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = negativeIncrement }) .Update().Execute(); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2).Execute().FirstOrDefault(); Assert.AreEqual(0, updatedCounter.Counter); }
private RowsResult AddRows(IEnumerable <CounterEntityWithLinqAttributes> counters) { return(counters.Aggregate(CounterEntityWithLinqAttributes.GetEmptyRowsResult(), (current, c) => c.AddRow(current))); }
public void LinqCounter_BatchTest() { var mappingConfig = new MappingConfiguration(); mappingConfig.Define(new Map <CounterEntityWithLinqAttributes>() .ExplicitColumns() .Column(t => t.KeyPart1) .Column(t => t.KeyPart2) .Column(t => t.Counter, map => map.AsCounter()) .PartitionKey(t => t.KeyPart1, t => t.KeyPart2) .TableName("linqcounter_batchtest_table") ); var counterTable = new Table <CounterEntityWithLinqAttributes>(Session, mappingConfig); counterTable.CreateIfNotExists(); var counter = new CounterEntityWithLinqAttributes { KeyPart1 = Guid.NewGuid(), KeyPart2 = 1, Counter = 1 }; var counter2 = new CounterEntityWithLinqAttributes { KeyPart1 = counter.KeyPart1, KeyPart2 = 2, Counter = 2 }; var batch = counterTable.GetSession().CreateBatch(BatchType.Counter); var update1 = counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 1 }) .Update(); var update2 = counterTable .Where(t => t.KeyPart1 == counter2.KeyPart1 && t.KeyPart2 == counter2.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 2 }) .Update(); batch.Append(update1); batch.Append(update2); batch.Execute(); VerifyBatchStatement( 1, new[] { "UPDATE linqcounter_batchtest_table SET Counter = Counter + ? WHERE KeyPart1 = ? AND KeyPart2 = ?", "UPDATE linqcounter_batchtest_table SET Counter = Counter + ? WHERE KeyPart1 = ? AND KeyPart2 = ?" }, new[] { new object[] { 1L, counter.KeyPart1, counter.KeyPart2 }, new object[] { 2L, counter2.KeyPart1, counter2.KeyPart2 } }); var expectedCounters = new[] { counter, counter2 }; PrimeLinqCounterRangeQuery(expectedCounters, "linqcounter_batchtest_table", false); var counters = counterTable.Execute().ToList(); Assert.AreEqual(2, counters.Count); Assert.IsTrue(counters.Contains(counter)); Assert.IsTrue(counters.Contains(counter2)); }
public void LinqAttributes_Counter_Increments(int increment) { // Create config that uses linq based attributes var mappingConfig = new MappingConfiguration(); var counterTable = new Table <CounterEntityWithLinqAttributes>(Session, mappingConfig); var counter = new CounterEntityWithLinqAttributes { KeyPart1 = Guid.NewGuid(), KeyPart2 = 1 }; var updateCounterCql = "UPDATE \"CounterEntityWithLinqAttributes\" " + "SET \"Counter\" = \"Counter\" + ? " + "WHERE \"KeyPart1\" = ? AND \"KeyPart2\" = ?"; // first update counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = increment }) .Update() .Execute(); VerifyBoundStatement(updateCounterCql, 1, (long)increment, counter.KeyPart1, counter.KeyPart2); counter.Counter = increment; // counter = increment PrimeLinqCounterQuery(counter); var updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Execute() .First(); Assert.AreEqual(increment, updatedCounter.Counter); // second update counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = increment }) .Update().Execute(); VerifyBoundStatement(updateCounterCql, 2, (long)increment, counter.KeyPart1, counter.KeyPart2); counter.Counter += increment; // counter = increment*2; PrimeLinqCounterQuery(counter); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Execute().First(); Assert.AreEqual(increment * 2, updatedCounter.Counter); // third update counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = increment }) .Update().Execute(); VerifyBoundStatement(updateCounterCql, 3, (long)increment, counter.KeyPart1, counter.KeyPart2); counter.Counter += increment; // counter = increment*3; PrimeLinqCounterQuery(counter); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Execute().First(); Assert.AreEqual(increment * 3, updatedCounter.Counter); // testing negative values var negativeIncrement = -1 * increment; // first negative update counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = negativeIncrement }) .Update().Execute(); VerifyBoundStatement(updateCounterCql, 1, (long)negativeIncrement, counter.KeyPart1, counter.KeyPart2); counter.Counter += negativeIncrement; PrimeLinqCounterQuery(counter); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Execute().First(); Assert.AreEqual(increment * 2, updatedCounter.Counter); // second negative update counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = negativeIncrement }) .Update().Execute(); VerifyBoundStatement(updateCounterCql, 2, (long)negativeIncrement, counter.KeyPart1, counter.KeyPart2); counter.Counter += negativeIncrement; // counter -= increment = increment PrimeLinqCounterQuery(counter); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Execute().First(); Assert.AreEqual(increment, updatedCounter.Counter); // third negative update counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = negativeIncrement }) .Update().Execute(); VerifyBoundStatement(updateCounterCql, 3, (long)negativeIncrement, counter.KeyPart1, counter.KeyPart2); counter.Counter += negativeIncrement; // counter -= increment = 0 PrimeLinqCounterQuery(counter); updatedCounter = counterTable.Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Execute().First(); Assert.AreEqual(0, updatedCounter.Counter); }
public void LinqAttributes_Counter_AttemptInsert() { // Create config that uses linq based attributes var mappingConfig = new MappingConfiguration(); mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(CounterEntityWithLinqAttributes), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(CounterEntityWithLinqAttributes))); var table = new Table<CounterEntityWithLinqAttributes>(_session, mappingConfig); table.Create(); CounterEntityWithLinqAttributes pocoAndLinqAttributesLinqPocos = new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal) 123, }; // Validate Error Message var e = Assert.Throws<InvalidQueryException>(() => _session.Execute(table.Insert(pocoAndLinqAttributesLinqPocos))); string expectedErrMsg = "INSERT statement(s)? are not allowed on counter tables, use UPDATE instead"; StringAssert.IsMatch(expectedErrMsg, e.Message); }
public void LinqAttributes_Counter_CauseInfiniteLoop() { //var mapping = new Map<PocoWithCounter>(); // don't remove this 'GetTable' call yet var table = _session.GetTable<CounterEntityWithLinqAttributes>(); table.Create(); List<CounterEntityWithLinqAttributes> counterPocos = new List<CounterEntityWithLinqAttributes>(); for (int i = 0; i < 10; i++) { counterPocos.Add( new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }); } int counterIncrements = 100; string updateStr = String.Format("UPDATE \"{0}\" SET \"{1}\"=\"{1}\" + 1 WHERE \"{2}\"=? and \"{3}\"=?", typeof(CounterEntityWithLinqAttributes).Name, "Counter", "KeyPart1", "KeyPart2"); var updateSession = _session.Prepare(updateStr); foreach (CounterEntityWithLinqAttributes pocoWithCounter in counterPocos) { var boundStatement = updateSession.Bind(new object[] { pocoWithCounter.KeyPart1, pocoWithCounter.KeyPart2 }); for (int j = 0; j < counterIncrements; j++) _session.Execute(boundStatement); pocoWithCounter.Counter += counterIncrements; } List<CounterEntityWithLinqAttributes> countersQueried = table.Select(m => m).Execute().ToList(); foreach (CounterEntityWithLinqAttributes pocoWithCounterExpected in counterPocos) { bool counterFound = false; foreach (CounterEntityWithLinqAttributes pocoWithCounterActual in countersQueried) { if (pocoWithCounterExpected.KeyPart1 == pocoWithCounterActual.KeyPart1) { Assert.AreEqual(pocoWithCounterExpected.KeyPart2, pocoWithCounterExpected.KeyPart2); Assert.AreEqual(pocoWithCounterExpected.Counter, pocoWithCounterExpected.Counter); counterFound = true; } } Assert.IsTrue(counterFound, "Counter with first key part: " + pocoWithCounterExpected.KeyPart1 + " was not found!"); } //var mapping = new Map<PocoWithCounter>(); table = _session.GetTable<CounterEntityWithLinqAttributes>(); table.Create(); CounterEntityWithLinqAttributes pocoAndLinqAttributesLinqPocos = new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }; // Validate Error Message var e = Assert.Throws<InvalidQueryException>(() => _session.Execute(table.Insert(pocoAndLinqAttributesLinqPocos))); string expectedErrMsg = "INSERT statement are not allowed on counter tables, use UPDATE instead"; Assert.AreEqual(expectedErrMsg, e.Message); _session.DeleteKeyspace(_uniqueKsName); _uniqueKsName = TestUtils.GetUniqueKeyspaceName(); _session.CreateKeyspace(_uniqueKsName); _session.ChangeKeyspace(_uniqueKsName); //var mapping = new Map<PocoWithCounter>(); counterPocos = new List<CounterEntityWithLinqAttributes>(); table = _session.GetTable<CounterEntityWithLinqAttributes>(); table.Create(); for (int i = 0; i < 100; i++) { counterPocos.Add( new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }); } counterIncrements = 2000; updateStr = String.Format("UPDATE \"{0}\" SET \"{1}\"=\"{1}\" + 1 WHERE \"{2}\"=? and \"{3}\"=?", typeof(CounterEntityWithLinqAttributes).Name, "Counter", "KeyPart1", "KeyPart2"); updateSession = _session.Prepare(updateStr); Parallel.ForEach(counterPocos, pocoWithCounter => { var boundStatement = updateSession.Bind(new object[] { pocoWithCounter.KeyPart1, pocoWithCounter.KeyPart2 }); for (int j = 0; j < counterIncrements; j++) _session.Execute(boundStatement); pocoWithCounter.Counter += counterIncrements; }); countersQueried = table.Select(m => m).Execute().ToList(); foreach (CounterEntityWithLinqAttributes pocoWithCounterExpected in counterPocos) { bool counterFound = false; foreach (CounterEntityWithLinqAttributes pocoWithCounterActual in countersQueried) { if (pocoWithCounterExpected.KeyPart1 == pocoWithCounterActual.KeyPart1) { Assert.AreEqual(pocoWithCounterExpected.KeyPart2, pocoWithCounterExpected.KeyPart2); Assert.AreEqual(pocoWithCounterExpected.Counter, pocoWithCounterExpected.Counter); counterFound = true; } } Assert.IsTrue(counterFound, "Counter with first key part: " + pocoWithCounterExpected.KeyPart1 + " was not found!"); } }
public void LinqAttributes_Counter_CauseInfiniteLoop() { //var mapping = new Map<PocoWithCounter>(); // don't remove this 'GetTable' call yet var table = _session.GetTable <CounterEntityWithLinqAttributes>(); table.Create(); List <CounterEntityWithLinqAttributes> counterPocos = new List <CounterEntityWithLinqAttributes>(); for (int i = 0; i < 10; i++) { counterPocos.Add( new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }); } int counterIncrements = 100; string updateStr = String.Format("UPDATE \"{0}\" SET \"{1}\"=\"{1}\" + 1 WHERE \"{2}\"=? and \"{3}\"=?", typeof(CounterEntityWithLinqAttributes).Name, "Counter", "KeyPart1", "KeyPart2"); var updateSession = _session.Prepare(updateStr); foreach (CounterEntityWithLinqAttributes pocoWithCounter in counterPocos) { var boundStatement = updateSession.Bind(new object[] { pocoWithCounter.KeyPart1, pocoWithCounter.KeyPart2 }); for (int j = 0; j < counterIncrements; j++) { _session.Execute(boundStatement); } pocoWithCounter.Counter += counterIncrements; } List <CounterEntityWithLinqAttributes> countersQueried = table.Select(m => m).Execute().ToList(); foreach (CounterEntityWithLinqAttributes pocoWithCounterExpected in counterPocos) { bool counterFound = false; foreach (CounterEntityWithLinqAttributes pocoWithCounterActual in countersQueried) { if (pocoWithCounterExpected.KeyPart1 == pocoWithCounterActual.KeyPart1) { Assert.AreEqual(pocoWithCounterExpected.KeyPart2, pocoWithCounterExpected.KeyPart2); Assert.AreEqual(pocoWithCounterExpected.Counter, pocoWithCounterExpected.Counter); counterFound = true; } } Assert.IsTrue(counterFound, "Counter with first key part: " + pocoWithCounterExpected.KeyPart1 + " was not found!"); } //var mapping = new Map<PocoWithCounter>(); table = _session.GetTable <CounterEntityWithLinqAttributes>(); table.Create(); CounterEntityWithLinqAttributes pocoAndLinqAttributesLinqPocos = new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }; // Validate Error Message var e = Assert.Throws <InvalidQueryException>(() => _session.Execute(table.Insert(pocoAndLinqAttributesLinqPocos))); string expectedErrMsg = "INSERT statement are not allowed on counter tables, use UPDATE instead"; Assert.AreEqual(expectedErrMsg, e.Message); _session.DeleteKeyspace(_uniqueKsName); _uniqueKsName = TestUtils.GetUniqueKeyspaceName(); _session.CreateKeyspace(_uniqueKsName); _session.ChangeKeyspace(_uniqueKsName); //var mapping = new Map<PocoWithCounter>(); counterPocos = new List <CounterEntityWithLinqAttributes>(); table = _session.GetTable <CounterEntityWithLinqAttributes>(); table.Create(); for (int i = 0; i < 100; i++) { counterPocos.Add( new CounterEntityWithLinqAttributes() { KeyPart1 = Guid.NewGuid(), KeyPart2 = (decimal)123, }); } counterIncrements = 2000; updateStr = String.Format("UPDATE \"{0}\" SET \"{1}\"=\"{1}\" + 1 WHERE \"{2}\"=? and \"{3}\"=?", typeof(CounterEntityWithLinqAttributes).Name, "Counter", "KeyPart1", "KeyPart2"); updateSession = _session.Prepare(updateStr); Parallel.ForEach(counterPocos, pocoWithCounter => { var boundStatement = updateSession.Bind(new object[] { pocoWithCounter.KeyPart1, pocoWithCounter.KeyPart2 }); for (int j = 0; j < counterIncrements; j++) { _session.Execute(boundStatement); } pocoWithCounter.Counter += counterIncrements; }); countersQueried = table.Select(m => m).Execute().ToList(); foreach (CounterEntityWithLinqAttributes pocoWithCounterExpected in counterPocos) { bool counterFound = false; foreach (CounterEntityWithLinqAttributes pocoWithCounterActual in countersQueried) { if (pocoWithCounterExpected.KeyPart1 == pocoWithCounterActual.KeyPart1) { Assert.AreEqual(pocoWithCounterExpected.KeyPart2, pocoWithCounterExpected.KeyPart2); Assert.AreEqual(pocoWithCounterExpected.Counter, pocoWithCounterExpected.Counter); counterFound = true; } } Assert.IsTrue(counterFound, "Counter with first key part: " + pocoWithCounterExpected.KeyPart1 + " was not found!"); } }