logo
down
shadow

Dynamic Linq statement not working in EF core


Dynamic Linq statement not working in EF core

Content Index :

Dynamic Linq statement not working in EF core
Tag : chash , By : user143038
Date : September 13 2020, 03:00 AM

seems to work fine Working on a asp net core 2.2 application. I want to dynamic order a query result. this is the code I have:
m => m.GetType().GetProperty(sortDataField).GetValue(m, null)
public static Expression<Func<TModel, object>> GetPropertyExpression<TModel>(this IEnumerable<TModel> model, string propertyName) {
    // Manually build the expression tree for 
    // the lambda expression m => m.PropertyName.

    // (TModel m) =>
    var parameter = Expression.Parameter(typeof(TModel), "m");
    // (TModel m) => m.PropertyName
    var property = Expression.PropertyOrField(parameter, propertyName);
    // (TModel m) => (object) m.PropertyName
    var cast = Expression.Convert(property, typeof(object));

    var expression = Expression.Lambda<Func<TModel, object>>(cast, parameter);
    return expression;
}
if (sortDataField != null) {
    //m => m.sortDataField
    var keySelector = Mut.GetPropertyExpression(sortDataField);

    if (sortOrder == "asc") {
        Mut = Mut.OrderBy(keySelector);
    } else {
        Mut = Mut.OrderByDescending(keySelector);
    }
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

C# SQL Statement transformed TO LINQ how can i translate this statement to a working linq


Tag : chash , By : picamiolo
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Make sure you reference System.Data.DataSetExtensions, and use the AsEnumerable() method to use LINQ to DataSets.
            var myTable1 = new [] { 
            new { CID = "123", AID = 345, Data = 32323, Status = 1, Language = "EN"},
            new { CID = "231", AID = 123, Data = 11525, Status = 2, Language = "EN"},
            new { CID = "729", AID = 513, Data = 15121, Status = 1, Language = "ANY"},
            new { CID = "231", AID = 123, Data = 54421, Status = 2, Language = "EN"}}
            .ToDataTable().AsEnumerable();

        var myTable2 = new [] { 
            new { CID = "512", AID = 513, Data = 32323, Status = 1, Language = "ANY"},
            new { CID = "444", AID = 123, Data = 11525, Status = 2, Language = "BLAH"},
            new { CID = "222", AID = 333, Data = 15121, Status = 1, Language = "ANY"},
            new { CID = "111", AID = 345, Data = 54421, Status = 2, Language = "EN"}}
            .ToDataTable().AsEnumerable();

         var myTable3 = new [] { 
            new { CID = "888", AID = 123, Data = 32323, Status = 2, Language = "EN"},
            new { CID = "494", AID = 333, Data = 11525, Status = 1, Language = "FR"},
            new { CID = "202", AID = 513, Data = 15121, Status = 1, Language = "EN"},
            new { CID = "101", AID = 345, Data = 54421, Status = 2, Language = "ANY"}}
            .ToDataTable().AsEnumerable();

         var q = from p in myTable1
                 join b in myTable2.Union(myTable3) on p.Field<int>("AID") equals b.Field<int>("AID")
                 where (b.Field<string>("Language") == "EN" || b.Field<string>("Language") == "ANY") && b.Field<int>("Status") == 1
                 select new
                 {
                     CID = p.Field<string>("CID"),
                     B_AID = p.Field<int>("AID"),
                     P_AID = b.Field<int>("AID"),
                     Data = b.Field<int>("Data"),
                     Status = b.Field<int>("Status"),
                     Language = b.Field<string>("Language")
                 };


         var table = q.ToDataTable();

Linq to NHibernate and Dynamic LINQ - query caching not working


Tag : linq , By : socurious
Date : March 29 2020, 07:55 AM
I hope this helps you . Ok, I know what is the reason.
Dynamic Linq doesn't use Parameter Names in Linq Expressions. E.g. if I want to sort using lambda statemant, I write:
query.OrderBy(item => item.Name)
query.OrderBy("Name")
query.OrderBy( => .Name)
if (parameters.Length == 1 && String.IsNullOrEmpty(parameters[0].Name))
if (parameters.Length == 1 && (parameters[0].Name == "it" || String.IsNullOrEmpty(parameters[0].Name)))
Expression.Parameter(source.ElementType, "")
Expression.Parameter(source.ElementType, "it")

How to write a statement that generates a LIKE T-SQL statement in System.Linq.Dynamic


Tag : chash , By : Tamizhvendan
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , System.Linq.Dynamic translates your text to regular LINQ expressions, and there is no concept of "LIKE" there. How would you write your like in regular LINQ? Something like that:
ctx.Entity.Where(c => c.Description.StartsWith("test"));
// @0 is first parameter, which is "test" in this case
ctx.Entity.Where("Description.StartsWith(@0)", "test"); 
ctx.Entity.Where("Description.StartsWith(\"test\")"); 

Linq to excel not working , when upgrading .net core 2.1 to .net core 2.2


Tag : asp.net-core , By : Stephen Dewar
Date : March 29 2020, 07:55 AM
this will help
I think linq to excel don't work in .net core 2.2. so better to use EPPlus package. Download EPPlus.Core from nuget package
Dictionary<object, string> leadExcelViewModels = new Dictionary<object, string>();

          using (ExcelPackage package = new ExcelPackage(file))
                {
                    ExcelWorksheet workSheet = package.Workbook.Worksheets[1];
                    int totalRows = workSheet.Dimension.Rows;//get total rows counts of excel file
                    int totalColumns = workSheet.Dimension.Columns;// get total columns count of excel file.

                    if (totalRows > 1)
                    {
                        for (int i = 2; i < totalRows; i++)
                        {
                            leadExcelViewModels = new Dictionary<object, string>();
                            leadModel = new YourViewModel();
                            for (int j = 1; j <= totalColumns; j++)
                            {
                                if (workSheet.Cells[i, j].Value != null)
                                    leadExcelViewModels.Add(workSheet.Cells[1, j].Value, workSheet.Cells[i, j].Value.ToString());
                                else
                                    leadExcelViewModels.Add(workSheet.Cells[1, j].Value, "0");
                            }

                            var js = JsonConvert.SerializeObject(leadExcelViewModels);
                            leadModel = JsonConvert.DeserializeObject<YourViewModel>(js);
                           // bind each leadModel to List of your YourViewModel list.


                        }
                    }
                }

EF Core 2.2 LINQ query not working in EF Core 3.0


Tag : entity-framework , By : jaset
Date : October 01 2020, 03:00 PM
I wish did fix the issue. This is due to one of the breaking changes in EF Core 3.0 and that is: LINQ queries are no longer evaluated on the client
So write the query in such way that EF Core can convert the expression into T-SQL or fetch the data into memory and then make your query.
Related Posts Related QUESTIONS :
  • c# wpf | create complex object with user-defined name to Serialize into JSON
  • How can I get a instance of a generic list with reflection?
  • WPF XAML - Design time and visibility of textbox
  • EF Core and MySql query is too slow
  • Getting Registered App Display Name from an App Id
  • How to get all variables from a string
  • Delete entity with all childs connected
  • Azure Build agent cant´t find class library referance
  • Initialize Nested Dictionaries in c#
  • .Net Core Binding
  • Generic event test method, preventing code duplication
  • How do I keep the ellipses in the center when the screen is resized
  • How to require a property using JsonSchema.NET?
  • C# XDocument Element/Elements returns null
  • Autofac keyed service with IEnumerable relationship type
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?
  • How to customize the Setup wizard with custom forms in Visual Studio setup project
  • C# ASP.NET - Use method from another class to create labels
  • C# List IList or IEnumerable as argument
  • Parsing File with C# And Replace method
  • Losing special unicode characters in encryption (C#)
  • Getting stored procedure returned value instead of row affected
  • How can I construct HTML using NameValuePair in android?
  • Loading a pop up page in ASP.net through a js file
  • How to pass alert or notification message from controller to View?
  • C# to pause, turn on ssas server, backup cube.... how to?
  • How to execute DataTable.Select() for a column of custom class type for a particular element in that C#
  • how to connect mysql8.0 with C#
  • Passing incorrect values into MultiValueConverter by MultiBinding
  • Can i use IEnumerator as Update func?
  • How to convert API Json response to C# Array?
  • Blazor Textfield Oninput User Typing Delay
  • Performing both layout and render transform results in wrong output
  • uwp beforetextchanged cursor moving in front of text
  • How to keep duplicates from a string[] exclude words from a List and print them out
  • .Net Core Strings.Asc/Mid/Chr/Len missing even after importing Microsoft.VisualBasic
  • How to return to previous search page without being asked to Confirm Form Re-submission and keeping the results on ASP.N
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • How to convert Word document created from template by OpenXML into MemoryStream?
  • How can I make a single slider that changes the color of an object?
  • Remap JSON parameter in c#
  • What is the difference between "this ref" and "ref this" when talking about C# 7.2 ref extension met
  • Convert OpenSSL encryption into native C#
  • Accessing Properties in Razor Pages
  • How to get SOAP element value
  • Projection after Group
  • C# error cannot convert sytem.text.regularexpressions.match to string
  • Issues with Save/Load System in a Text Based Adventure game made with ScriptableObjects in Unity
  • VS2019 MSBuild.exe - ASP .Net MVC project fails to publish when using PublishProfile, but works when using OutDir parame
  • Does <pages validateRequest="false" /> in Web.config still matter?
  • How to send new request to redirect URL with new access token
  • Attempt to invoke virtual method on a null object reference Xamarin LockScreen
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com