Base solution for your next web application
Open Closed

update with where clause #1107


User avatar
0
iamnish created

while updating a record I want o add where clause .I dont want to use the default primary keys as declared in entity . :?:


8 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    EntityFramework itself has not such a feature. You can try to use 3rd party libraries, like <a class="postlink" href="https://github.com/loresoft/EntityFramework.Extended">https://github.com/loresoft/EntityFramework.Extended</a>

  • User Avatar
    0
    iamnish created

    can it be used with abp repositories?

  • User Avatar
    0
    hikalkan created
    Support Team

    You can add custom methods to your repositories (<a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/EntityFramework-Integration#DocCustomRepositoryMethods">http://www.aspnetboilerplate.com/Pages/ ... oryMethods</a>)

  • User Avatar
    0
    iamnish created

    using <a class="postlink" href="https://github.com/loresoft/EntityFramework.Extended">https://github.com/loresoft/EntityFramework.Extended</a> gives following errors

    Error 4 'System.Linq.IQueryable' does not contain a definition for 'PageBy' and no extension method 'PageBy' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?) C:\Users\nishi_000\Desktop\projects\diam\Ni.diam.Application\MultiTenancy\TenantAppService.cs 44

    Error 8 'System.Linq.IQueryable' does not contain a definition for 'PageBy' and no extension method 'PageBy' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?) C:\Users\nishi_000\Desktop\projects\diam\Ni.diam.Application\Authorization\Users\UserAppService.cs 66

    Error 11 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable<Ni.diam.Authorization.Users.LinkedUserDto>'. An explicit conversion exists (are you missing a cast?) C:\Users\nishi_000\Desktop\projects\diam\Ni.diam.Application\Authorization\Users\UserLinkAppService.cs 121 Error 13 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable<Ni.diam.Localization.Dto.LanguageTextListDto>'. An explicit conversion exists (are you missing a cast?) C:\Users\nishi_000\Desktop\projects\diam\Ni.diam.Application\Localization\LanguageAppService.cs 163

  • User Avatar
    0
    hikalkan created
    Support Team

    Add "using Abp.Linq.Extensions;" to your code file in order to access PageBy extension method.

  • User Avatar
    0
    iamnish created

    I added that now error is with OrderBy

    query.OrderBy(input.Sorting)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    There is an open issue about this problem for EntityFramework.Extended library on github. <a class="postlink" href="https://github.com/loresoft/EntityFramework.Extended/issues/114">https://github.com/loresoft/EntityFrame ... issues/114</a>

    For now it seems, it's not possible to use EntityFramework.Extended library.

    You can fork EntityFramework.Extended library and change the name of OrderBy method but it will bring you an extra work for keeping synchronized your version with EntityFramework.Extended master.

  • User Avatar
    0
    hikalkan created
    Support Team

    OK, it seems there are two extension method with same name in same namespace and same class, but in different assemblies. We can use some reflection to call OrderBy in a desired assembly.

    1. Create your own OrderBy extension method:
    public static class DynamicQueryableExtensions
        {
            private static readonly MethodInfo _orderByMethod;
    
            static DynamicQueryableExtensions()
            {
                var dynamicQueryableType = Type.GetType("System.Linq.Dynamic.DynamicQueryable, System.Linq.Dynamic");
                _orderByMethod = GetMethodEx(dynamicQueryableType,
                    "OrderBy",
                    new[]
                    {
                        typeof (IQueryable<>),
                        typeof(string),
                        typeof(object[])
                    },
                    1);
            }
    
            public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values)
            {
                return (IQueryable<T>) _orderByMethod
                    .MakeGenericMethod(typeof (T))
                    .Invoke(null, new object[] {source, ordering, values});
            }
    
            public static MethodInfo GetMethodEx(this Type type, string name, Type[] parameterTypes, int genericTypeCount = 0)
            {
                var methods = type.GetMethods();
    
                foreach (var method in methods.Where(m => m.Name == name))
                {
                    if (method.IsGenericMethodDefinition && method.GetGenericArguments().Length != genericTypeCount)
                    {
                        continue;
                    }
    
                    var methodParameterTypes = method.GetParameters().Select(p => p.ParameterType);
                    if (!methodParameterTypes.SequenceEqual(parameterTypes, new SimpleTypeComparer()))
                    {
                        continue;
                    }
    
                    return method;
                }
    
                return null;
            }
    
            private class SimpleTypeComparer : IEqualityComparer<Type>
            {
                public bool Equals(Type x, Type y)
                {
                    return x.Assembly == y.Assembly &&
                        x.Namespace == y.Namespace &&
                        x.Name == y.Name;
                }
    
                public int GetHashCode(Type obj)
                {
                    throw new NotImplementedException();
                }
            }
        }
    

    Create this class in a namespace in your application, say MyCompanyName.MyProjectName.Extensions namespace. I suggest you to add into Core project

    1. Remove "using System.Linq.Dynamic;" from your code and add "using MyCompanyName.MyProjectName.Extensions;".

    That's all. It will work.