SevenMgCreatine
Why event is not triggered correctly?
services.AddScoped<IUnitOfWork>(provider =>
{
var uow = new UnitOfWork();
var remoteSyncHandler = provider.GetRequiredService<RemoteSyncHandler>();
uow.EntitySaved += remoteSyncHandler.OnChangesSaved;
return uow;
});
and just register RemoteSyncHandler and the SyncAgent as Scoped too.
9 replies
LINQ: Duplicate key after GroupBy
Yeah, I needed some time to figure this out. I had three options to solve it:
1. Change the collasion in the data base schema to something that doesent ignores whitespaces (e.g. SQL_Latin1_General_Pref_CP1_CI_AS)
2. The MS way would be something like this: (https://learn.microsoft.com/de-de/ef/core/miscellaneous/collations-and-case-sensitivity)
var userGroupDict = rechteDbModel.Um_Gruppenzuweis
.AsNoTracking()
.Select(gz => new
{
CollatedUserid = EF.Functions.Collate(gz.Userid, "SQL_Latin1_General_Pref_CP1_CI_AS"),
gz.Um_Usergr_Id
})
.GroupBy(x => x.CollatedUserid)
...
3. In my case I just materialized before the group by and used the group by of IEnumerable.
13 replies
LINQ: Duplicate key after GroupBy
Yeah...I though so too. That why I first checked if I could create a query with the "same" keys...I looked at the sql query with the sql profiler and the .Select(...) does already materialize the query.
var userGroupDict = rechteDbModel.Um_Gruppenzuweis
.AsNoTracking()
.Select(gz => new { gz.Userid, gz.Um_Usergr_Id })
.GroupBy(gz => gz.Userid.Trim())
.ToDictionary(g => g.Key, g => g.Select(gz => gz.Um_Usergr_Id).ToList());
result in the following query:
SELECT [t].[Key], [t].[Userid], [t].[Um_Usergr_Id]
FROM (
SELECT [u].[USERID] AS [Userid], [u].[UM_USERGR_ID] AS [Um_Usergr_Id], LTRIM(RTRIM([u].[USERID])) AS [Key]
FROM [dbo].[UM_GRUPPENZUWEIS] AS [u]
) AS [t]
ORDER BY [t].[Key]
And without the trim, this query is performed:
SELECT [u].[USERID], [u].[UM_USERGR_ID] AS [Um_Usergr_Id]
FROM [dbo].[UM_GRUPPENZUWEIS] AS [u]
ORDER BY [u].[USERID]
13 replies
LINQ: Duplicate key after GroupBy
Yes, both "michael" and "MICHAEL" where recognized as the same group on the database level and when using the IEnumerable GroupBy (which uses the OrdinalIgnoreCase-Comparer on default).
But the IEnumerable-GroupBy does not ignore whitespaces and 'michael ' and 'michael' where recognized as different groups, but not different keys for the dictionary. One working solution is to trim the userid. Another one would be to group on the sql server (which irgnores the case and trailing whitespaces (but not leading ones))...but I will chose to just trim the Userid in the groupby, because I dont know how different databases behave.
Thanks for pointing me in the right direction. 🙂
13 replies
LINQ: Duplicate key after GroupBy
Hey. Yes, UserId is a string...the database is MSSQL. I already tried to set up a query that produces two groups with the "same" key. But if I just change the case (e.g. "michael" and "MICHAEL" for the userid) it just creates one group. Empty spaces are also getting ignored in a normal query with GROUP BY userid.
13 replies