linq2db.EntityFrameworkCore icon indicating copy to clipboard operation
linq2db.EntityFrameworkCore copied to clipboard

Parameter introduced in switch case guard can't be used in linq predicates

Open derigel23 opened this issue 4 years ago • 0 comments

Hi!

I've modified your test case to demonstrate the problem.

The following code will fail with the exception Incorrect syntax near '<'. Must declare the scalar variable "@". If you copy a parameter to local variable and use it then - all is fine.

		public async Task TestTransaction([Values(true, false)] bool enableFilter)
		{
			using (var ctx = CreateContext(enableFilter))
			{
				using (var transaction = ctx.Database.BeginTransaction())
				using (var db = ctx.CreateLinqToDbConnection())
				{

					var test1 = await ctx.Products.Where(p => p.ProductName.StartsWith("U")).MaxAsync(p => p.QuantityPerUnit);
					var test2 = await ctx.Products.Where(p => p.ProductName.StartsWith("U")).MaxAsyncLinqToDB(p => p.QuantityPerUnit);

					Assert.AreEqual(test1, test2);

					bool GetParameter(out string parameter)
					{
						parameter = "a";
						return true;
					}

					switch (true)
					{
						case true when GetParameter(out var parameter):
							ctx.Products.Where(p => p.ProductName == parameter)
								.ToLinqToDB(db)
								.Delete();
							break;							
					}
					

					transaction.Rollback();
				}
			}
		}

derigel23 avatar Dec 15 '21 18:12 derigel23