Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringAggregate with GroupBy on SQL Server #4501

Open
brianvonkuster opened this issue May 8, 2024 · 2 comments
Open

StringAggregate with GroupBy on SQL Server #4501

brianvonkuster opened this issue May 8, 2024 · 2 comments

Comments

@brianvonkuster
Copy link

brianvonkuster commented May 8, 2024

Hi,

I've been running into an issue when trying to use StringAggregate with a GroupBy clause when executing against SQL Server (2017, 2019, 2022).

When I run the equivalent of the following code:

public class ProductInfo
{
	public int Id { get; set; }
	public string AggregatedDescription { get; set; }
}

public class Product {
	[Column]
	public int Id { get; set; }
	
	[Column]
	public string Description { get; set; }
}

public class ProductService {
	public ITable<Product> Products => GetTable<Product>();

	public IEnumerable<ProductInfo> GetAggregatedDescriptions() 
	{
		var productsGrouped = Products
			.GroupBy(x => x.Id)
			.Select(g => new ProductInfo {
				Id = g.Key,
				AggregatedDescription = g.StringAggregate(", ", x => x.Description)
			});
			
		return productsGrouped.ToList();
	}
}

I get an exception thrown --

LinqToDB.LinqToDBException: 'You should explicitly specify selected fields for server-side GroupBy() call or add AsEnumerable() call before GroupBy() to perform client-side grouping.
Set Configuration.Linq.GuardGrouping = false to disable this check.
Additionally this guard exception can be disabled by extension GroupBy(...).DisableGuard() or using options.UseGuardGrouping(false) configuration extension.

Disabling the guard doesn't seem to help, as Linq2Db throws an exception saying StringAggregate is a serverside function. Not sure if this is a potential bug or I'm doing something wrong, I've tried several permutations including g.StringAggregate(", ", x => x.Description).ToValue() to no avail.

@sdanyliv
Copy link
Member

sdanyliv commented May 8, 2024

Looks like you have returned tuple (int Id, string AggregatedDescription), but query is written using anonymous class. Where is true?

g.StringAggregate(", ", x => x.Description).ToValue() - should be right.

@brianvonkuster
Copy link
Author

brianvonkuster commented May 8, 2024

Whoops, sorry. I was cutting a class out and messed that up. I edited the example to be fully functional now.

I tried g.StringAggregate(", ", x => x.Description).ToValue(), however when I do that I get the exception LinqToDB.Linq.LinqException: ''g.StringAggregate(", ", x => x.Description).ToValue()' cannot be converted to SQL.'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants