public DatabaseEntity CreateEmptyEntity(DatabaseTableDefinitionJson definition, DatabaseKey key, bool phantomEntity) { Dictionary <string, IDatabaseField> columns = new(StringComparer.InvariantCultureIgnoreCase); foreach (var column in definition.Groups.SelectMany(t => t.Fields) .Distinct( EqualityComparerFactory.Create <DatabaseColumnJson>( f => f.DbColumnName.GetHashCode(), (a, b) => a !.DbColumnName.Equals(b !.DbColumnName)))) { IValueHolder valueHolder; var type = column.ValueType; if (parameterFactory.IsRegisteredLong(type)) { type = "uint"; } else if (parameterFactory.IsRegisteredString(type)) { type = "string"; } else if (type.EndsWith("Parameter")) { type = "uint"; } if (type == "float") { valueHolder = new ValueHolder <float>(column.Default is float f ? f : 0.0f, column.CanBeNull && column.Default == null); } else if (type is "int" or "uint" or "long") { valueHolder = new ValueHolder <long>(column.Default is long f ? f : 0, column.CanBeNull && column.Default == null); }
public async Task <IList <PermissionRoleModel> > GetAllUserPermissionsWithRolesAsync(long userId) { var user = await SmartHomeAppDbContext.SingleAsync <User>(userId); var userPermissions = await SmartHomeAppDbContext.Query <UserPermission>() .Where(x => x.UserId == userId) .ProjectTo <PermissionRoleModel>(Mapper.ConfigurationProvider) .ToListAsync(); var rolesStrings = (List <string>) await userManager.GetRolesAsync(user); var rolePermissions = await SmartHomeAppDbContext.Query <RolePermission>() .Where(x => rolesStrings.Contains(x.Role.Name)) .ProjectTo <PermissionRoleModel>(Mapper.ConfigurationProvider) .ToListAsync(); var comparer = EqualityComparerFactory.Create <PermissionRoleModel>( x => (int)x.PermissionId, (x, y) => x.PermissionId == y.PermissionId && string.Equals(x.RoleName, y.RoleName)); var resultPermissions = userPermissions.Union(rolePermissions, comparer); resultPermissions = resultPermissions.OrderBy(x => x.PermissionName); return(resultPermissions.ToList()); }
public bool Exists(User user) { //Get(user.Id); IEqualityComparer <User> userComparer = EqualityComparerFactory <User> .Create((user_a, user_b) => user_a.Username == user_b.Username, (_user) => _user.GetHashCode()); return(dbset.ToList().Contains(user, userComparer)); }
public static IImmutableSession WithRegexComparer(this IImmutableSession context) { return(context.WithComparer(GetComparerNameFromCaller(), EqualityComparerFactory <object> .Create ( equals: (left, right) => Regex.IsMatch((string)right, (string)left, RegexOptions.None), getHashCode: (obj) => 0 ))); }
public static IImmutableSession WithSoftStringComparer(this IImmutableSession context) { return(context.WithComparer(GetComparerNameFromCaller(), EqualityComparerFactory <object> .Create ( equals: (left, right) => SoftString.Comparer.Equals((string)left, (string)right), getHashCode: (obj) => SoftString.Comparer.GetHashCode((string)obj) ))); }
private ProjectionGatewayConfigurationBuilder(ILocator locator) { _locator = locator; var stringEqComparer = EqualityComparerFactory.Create <string>( (x, y) => x.ToLower() == y.ToLower(), s => s.GetHashCode()); Subscriptions = new MultiValueDictionary <string, Subscription>(stringEqComparer); }
public static void AddUserSkillIfDoesNotExist(User dbUser, Skill userSkill) { // Update user if programming skill is missing IEqualityComparer <Skill> compareSkills = EqualityComparerFactory <Skill> .Create((s1, s2) => s1.Type == s2.Type, s => s.GetHashCode()); if (!dbUser.Skill.ToList().Contains(userSkill, compareSkills)) { Console.WriteLine($"dbUser does not contain skill: { userSkill }"); dbUser.Skill.Add(userSkill); } }
public XMerge(IEnumerable <KeyValuePair <string, string> > keyMap) { _keyMap = keyMap.ToDictionary( x => x.Key.Split('/'), x => new Func <XElement, string>(e => e.Attribute(x.Value).Value), EqualityComparerFactory <IEnumerable <string> > .Create( (left, right) => left.SequenceEqual(right), (obj) => obj.CalcHashCode() ) ); }
public DatabaseEntity CreateEmptyEntity(DatabaseTableDefinitionJson definition, uint key) { Dictionary <string, IDatabaseField> columns = new(); foreach (var column in definition.Groups.SelectMany(t => t.Fields) .Distinct( EqualityComparerFactory.Create <DatabaseColumnJson>( f => f.DbColumnName.GetHashCode(), (a, b) => a !.DbColumnName.Equals(b !.DbColumnName)))) { IValueHolder valueHolder; var type = column.ValueType; if (parameterFactory.IsRegisteredLong(type)) { type = "uint"; } else if (parameterFactory.IsRegisteredString(type)) { type = "string"; } else if (type.EndsWith("Parameter")) { type = "uint"; } if (type == "float") { valueHolder = new ValueHolder <float>(column.Default is float f ? f : 0.0f, column.CanBeNull && column.Default == null); } else if (type == "int" || type == "uint") { if (column.DbColumnName == definition.TablePrimaryKeyColumnName) { valueHolder = new ValueHolder <long>(key, false); } else { valueHolder = new ValueHolder <long>(column.Default is long f ? f : 0, column.CanBeNull && column.Default == null); } } else { valueHolder = new ValueHolder <string>(column.Default is string f ? f : "", column.CanBeNull && column.Default == null); } columns[column.DbColumnName] = databaseFieldFactory.CreateField(column.DbColumnName, valueHolder); } return(new DatabaseEntity(false, key, columns, null)); }
public Wallet(IEnumerable <Coin> coins) { Contract.Requires(coins != null); var coinEqualityComparer = EqualityComparerFactory.Create <Coin>( (c1, c2) => c1.ParValue == c2.ParValue, c => c.ParValue.GetHashCode()); _coins = coins .GroupBy(c => c.ParValue) .Select(c => c.Aggregate(0, (i, coin) => i + coin.Count, count => new Coin(c.Key, count))) .ToImmutableHashSet(coinEqualityComparer); }
public BagOfGoods(IEnumerable <Goods> goods) { Contract.Requires(goods != null); var goodsEqualityComparer = EqualityComparerFactory.Create <Goods>( (g1, g2) => g1.Identity.Equals(g2.Identity) && g1.Price == g2.Price, g => g.Identity.GetHashCode() ); _bag = goods .GroupBy(g => new { g.Identity, g.Price }) .Select(g => g.Aggregate( default(int), (count, nextGoods) => count + nextGoods.Count, c => new Goods(g.Key.Identity, g.Key.Price, c))) .ToHashSet(goodsEqualityComparer); }
/// <summary> /// Adds, removes and updates contacts, groups, masages /// </summary> /// <param name="syncData"></param> public static async Task SyncData() { if (!ServerConnected) { return; } await Task.Run(() => { var res = commandChain.MakeRequest(CommandType.Synchronize, null, User); if (res == null) { return; } //Update user if (User.LastTimeUpdated != res.UserData.LastTimeUpdated) { User = res.UserData; } var syncData = (object[])res.RequestData; var groupsInfo = (List <Group>)syncData[0]; var contactsInfo = (List <Contact>)syncData[1]; var messagesInfo = (List <Message>)syncData[2]; //Find differences in Last Time Update and update if necessary foreach (var group in groupsInfo) { var foundGroup = Database.GroupsTableRepo.FindFirst(GroupsTableFields.Id.ToString(), group.Id.ToString()); if (foundGroup == null) { Database.GroupsTableRepo.Add(group); continue; } if (foundGroup.LastTimeUpdated != group.LastTimeUpdated) { Database.GroupsTableRepo.Update(GroupsTableFields.Id.ToString(), group.Id.ToString(), group); } } foreach (var contact in contactsInfo) { var foundContact = Database.ContactsTableRepo.FindFirst(ContactsTableFields.Id.ToString(), contact.Id.ToString()); if (foundContact == null) { Database.ContactsTableRepo.Add(contact); continue; } if (foundContact.LastTimeUpdated != contact.LastTimeUpdated) { Database.ContactsTableRepo.Update(ContactsTableFields.Id.ToString(), contact.Id.ToString(), contact); } } foreach (var message in messagesInfo) { var foundMessage = Database.MessagesTableRepo.FindFirst(MessagesTableFields.Id.ToString(), message.Id.ToString()); if (foundMessage == null) { Database.MessagesTableRepo.Add(message); continue; } if (foundMessage.LastTimeUpdated != message.LastTimeUpdated) { Database.MessagesTableRepo.Update(MessagesTableFields.Id.ToString(), message.Id.ToString(), message); } } #region Create custom comparers //Create comparers var groupsComparer = EqualityComparerFactory.Create <Group>( (Group a) => a.Id.GetHashCode(), (Group a, Group b) => a.Id == b.Id ); var contactsComparer = EqualityComparerFactory.Create <Contact>( (Contact a) => a.Id.GetHashCode(), (Contact a, Contact b) => a.Id == b.Id ); var messagesComparer = EqualityComparerFactory.Create <Message>( (Message a) => a.Id.GetHashCode(), (Message a, Message b) => a.Id == b.Id ); #endregion //Delete var deletedGroups = Database.GroupsTableRepo.GetAll()?.Except(groupsInfo, groupsComparer); var deletedContacts = Database.ContactsTableRepo.GetAll()?.Except(contactsInfo, contactsComparer); var deletedMessages = Database.MessagesTableRepo.GetAll()?.Except(messagesInfo, messagesComparer); if (deletedGroups != null) { foreach (var group in deletedGroups) { Database.GroupsTableRepo.Remove(GroupsTableFields.Id.ToString(), group.Id.ToString()); } } if (deletedContacts != null) { foreach (var contact in deletedContacts) { Database.ContactsTableRepo.Remove(GroupsTableFields.Id.ToString(), contact.Id.ToString()); } } if (deletedMessages != null) { foreach (var message in deletedMessages) { Database.MessagesTableRepo.Remove(GroupsTableFields.Id.ToString(), message.Id.ToString()); } } }); }
public static bool SequenceEqual <T>(IEnumerable <T> first, IEnumerable <T> second, Func <T?, T?, bool> equalizer) { return(Enumerable.SequenceEqual(first, second, EqualityComparerFactory.Create <T>((t) => t?.GetHashCode() ?? 0, equalizer))); }
public IEnumerable <ITagSpan <ModuleDeclarationTag> > GetTags(NormalizedSnapshotSpanCollection spans) { if (spans.Count == 0) { return(EmptyTags); } var proj = GetActiveProject(); var tests = new List <string>(); foreach (ProjectItem projItem in proj.ProjectItems) { tests.AddRange(GetModules(projItem)); } if (!tests.Any(x => x.Equals(FilePath(), StringComparison.OrdinalIgnoreCase))) { return(EmptyTags); } RefTable.Instance.TryRemoveDeclarations(FilePath()); var wholeText = spans.First().Snapshot.GetText(); var compilationUnit = Compiler.Instance.CompileModuleOnFly(wholeText, FilePath()).OnlyValidModules(); if (_previousUnits == null) { _previousUnits = compilationUnit.ValidModules; foreach (var validModule in compilationUnit.ValidModules) { ProcessNewModule(validModule); } } else { Func <ModuleRegistration, ModuleRegistration, bool> equals = (l, r) => l.Name.Equals(r.Name, StringComparison.OrdinalIgnoreCase); Func <ModuleRegistration, int> getHashCode = m => m.Name.ToUpperInvariant().GetHashCode(); var comparer = EqualityComparerFactory.Create(equals, getHashCode); var(newModules, sameModules, removedModules) = compilationUnit.ValidModules.SplitD(_previousUnits, comparer); foreach (var validModuleRegistration in newModules) { ProcessNewModule(validModuleRegistration); } foreach (var validModuleRegistration in removedModules) { _modulePool .TryRelease(new FileCompilationUnit <string>(FilePath(), validModuleRegistration.Key.Name)) .WhenHasValueThen(ModuleHub.Instance.ModuleRemoved); } foreach (var sameModule in sameModules) { var fUnit = new FileCompilationUnit <ModuleRegistration>(FilePath(), sameModule.leftIntersection.Key); _modulePool.Request(fUnit); } foreach (var sameModule in sameModules) { var fUnit = new FileCompilationUnit <ModuleRegistration>(FilePath(), sameModule.leftIntersection.Key); //if (sameModule.leftIntersection.Occurences.Count > 1 || // RefTable.Instance.HasDuplicates(sameModule.leftIntersection.Key.Name)) //{ // continue; //} var(duplicated, module) = _modulePool.Request(fUnit); if (duplicated) { continue; } var left = sameModule.leftIntersection.Key; var right = sameModule.rightIntersection.Key; module.Dependencies.Clear(); left .ClassRegistrations .Select(x => new PlainDependency(x.Dependency, x.Implementation)) .ForEach(newDep => module.Add(newDep)); left .ModuleRegistrations .ForEach(newModule => _modulePool.TryRequest(newModule.Name).WhenHasValueThen(m => module.Add(m))); //right // .ClassRegistrations // .Select(x => new PlainDependency(x.Dependency, x.Implementation)) // .ForEach(oldDep => module.Remove(oldDep)); //left // .ClassRegistrations // .Select(x => new PlainDependency(x.Dependency, x.Implementation)) // .ForEach(newDep => module.Add(newDep)); //right // .ModuleRegistrations // .ForEach(oldModule => _modulePool.TryRequest(oldModule.Name).WhenHasValueThen(m => module.Remove(m))); //left // .ModuleRegistrations // .ForEach(newModule => _modulePool.TryRequest(newModule.Name).WhenHasValueThen(m => module.Add(m))); } } _previousUnits = compilationUnit.ValidModules; return(EmptyTags); }