dynamic-linq

C# LINQ - How to build Group By clause dynamically

 ̄綄美尐妖づ 提交于 2019-11-29 01:15:32
问题 I am working on the application where user can select columns he/she wants to see on the screen and which columns to group by or aggregate. So, in my LINQ section I should actually pass variables that hold column names to both group by and aggregate clause. Keep in mind that DataTable dt may hold different data every time(e.g. Employee info, Purchase orders, Performance stats, etc). I can only get information about the data at run time via dt.Columns[i].ColumnName and dt.Columns[i].DataType

Querying Entity with LINQ using Dyanmic Field Name

你说的曾经没有我的故事 提交于 2019-11-29 00:44:35
I have created a dynamic search screen in ASP.NET MVC. I retrieved the field names from the entity through reflection so that I could allow the user to choose which fields they wanted to search on instead of displaying all fields in the view. When the search result is Posted back to the controller, I receive a FormCollection containing the FieldName and the Value. I don't know how many fields are being searched on, and the FormCollection only contains fields that were chosen by the user. I want to be able to now take that field name and apply that to my LINQ statement when I query the database

Dynamic LINQ - Is There A .NET 4 Version?

左心房为你撑大大i 提交于 2019-11-28 17:01:56
I'm looking to use LINQ for some searching routines and wanted to have some dynamic where clauses. So, for example, if a user wants to search by city or search by state, I would have a dynamic LINQ Where<> call instead of creating two strongly typed LINQ expressions and then using the appropriate one based on how the user wants to search. So I would like to do this: String criteria="p.City='Pittsburgh'"; //or "p.State='PA'" personData.Where(criteria) instead of personData.Where(p => p.City=="Pittsburgh"); or personData.Where(p => p.State=="PA"); I came across a blog post by Scott Guthrie

Counting percent of rows with category out of total number of rows using Dynamic Linq

女生的网名这么多〃 提交于 2019-11-28 14:35:28
How to achieve this using Dynamic Linq Join and Dynamic Linq SelectMany Desired result is in q and q2. q shows combination of aggregates from different levels of grouping in one expression. q2 uses calculated agg from prev levels and generates shorter SQL query. var grp=(db.A as IQueryable).GroupBy("grp","it"); var grp2=grp.Select("new(key as key,new(count() as cnt) as agg)"); foreach(dynamic r in grp2) Console.WriteLine("grp:"+r.key+"\tcnt:"+r.agg.cnt); //foreach(dynamic r in grp) Console.WriteLine(r.Key+"\t"+r.Count()); // throws RuntimeBinderException Console.WriteLine("--------------------

LINQ Dynamic Query Library

淺唱寂寞╮ 提交于 2019-11-28 10:15:26
I am building an ASP.Net MVC 3 application with Entity Framework 4. When the two pieces of code below are executed, both variables (query1 and query2) have a return type of System.Data.Objects.ObjectQuery<Asset.Model.Equipment> Query1 uses a direct instance of the ObjectContext, however, Query2 uses a repository pattern, ie, it calls GetEquipment in EquipmentService, which in turns calls the same named method in Equipment Repository. Both the methods in the Service and Repository return IQueryable<Equipment> How, here's my question, how come query2 will only work when I include using System

How can I dynamically select my Table at runtime with Dynamic LINQ

落爺英雄遲暮 提交于 2019-11-28 06:44:42
问题 I'm developing an application to allow engineers to conduct simple single table/view queries against our databases by selecting Database, Table, Fields. I get how to use the Dynamic LINQ Library Sample to provide for dynamically selecting the Select, Where and Order by Clauses at runtime but I'm at an impass on how to allot for table choice. Is there a way to provide for dynamically selecting the "from" table at run time, and if how could you provide some concrete example or point me in the

how to get value of a definite Column name in c# / Linq?

眉间皱痕 提交于 2019-11-28 06:28:02
问题 I want to know if it possible to get value of a definite columns name ? For exemple SELECT NAME FROM PERSON; string NAME; <--- Column name string fundPerson; fundPerson = context.PERSON.Where(a => a... == NAME); The problem is that the column name can change, it is not always the "NAME" column. Hence I need a solution where I can dynamically pass a column name to the query. 回答1: You want to the source code in C# to create a linq query that compiles to SELECT NAME FROM PERSON under Linq to SQL

Converting Expression<T, bool> to String

巧了我就是萌 提交于 2019-11-28 05:45:17
I need a way to recreate dynamically generated reports at some point in the future. Long story short, I need to store a specific linq query (different for each report) into database and then execute the query with dynamic Linq later on. This is all good, but I can't find a way to convert expression to string. As in: Expression<Func<Product, bool>> exp = (x) => (x.Id > 5 && x.Warranty != false); should become: "Product.Id > 5 && Product.Warranty != false" Is there a way to do that? This may not be the best/most efficient method, but it does work. Expression<Func<Product, bool>> exp = (x) => (x

Entity Framework + DayOfWeek

送分小仙女□ 提交于 2019-11-28 01:22:47
Using the System.Linq.Dynamic (managed here https://github.com/kahanu/System.Linq.Dynamic ), I am trying to capture the DayOfWeek field found on the DateTime for aggregation purposes using Entity Framework 6 (or greater). Previously asked for something similar, which helped a lot, Dynamic Linq + Entity Framework: datetime modifications for dynamic select Entity Framework supports getting the DayOfWeek using the SqlFunctions.DatePart("dw", datetime?) or we could do something a little more desired using something like DbFunctions.DiffDays(date?, date?). Idea: Getting the DayOfWeek in Linq to

How to use GroupBy using Dynamic LINQ

亡梦爱人 提交于 2019-11-27 23:50:14
I am trying to do a GroupBy using Dynamic LINQ but have trouble getting it to work. This is some sample code illustrating the problem: List<dtoMyAlbum> listAlbums = new List<dtoMyAlbum>(); for (int i = 0; i < 5000; i++) { dtoMyAlbum album = new dtoMyAlbum { Author = "My Author", BookID = i, CurrSymbol = "USD", Price = 23.23, Shop = i % 3 == 0 ? "TESCO" : "HMV" }; listAlbums.Add(album); } IQueryable<dtoMyAlbum> mydata = listAlbums.AsQueryable(); int count = mydata.Count(); //var mydataGrouped = mydata.GroupBy(a => a.Shop); // <-- this works well (but is not dynamic....) var mydataGrouped =