you can use reflection if you are using linq to objects, for instance you can use this:
.groupby(c => c.gettype().getproperty(category).getvalue(c, null))
if you are using linq to sql then an alternative is to use dynamic queries, check this link
you may build expression dynamically:
expression<func<customer,t>> buildexpression<t>(string category)
//first build parameter of lambda ( 'c =>' part of expression)
parameterexpression param = expression.parameter(typeof(customer), "c");
//then body of expression : ' => c.category'
expression<func<customer, t>> exp = expression.lambda<func<customer, t>>(expression.property(param, category), param);
and finally, call
well, sorry you still have to know the type of property to give t type parameter to
there are ways to do this, using for example
getproperty(category).propertytype and then call
gettype().getmethod("buildexpression<>"), but this requires a little more work.
anyway, you'll have to find a way to build
customerreportsummaryviewmodel from this type.
i don't know your use case, but you maybe all categories properties are of the same type, so you could hard-code it ?
if you are interested, and can't find a proper way to do it let me know i'll try to write a proper solution.
- LINQ group by query using reflected property name
- C# - code to order by a property using the property name as a string
- Query and updating a property in a collection using LINQ
- Filling in missing dates using a linq group by date query
- Problem with LINQ to Entities query using Sum on child object property
- Linq query using group by and having
- Linq - group by using the elements inside an array property
- linq orderby using property name string
- Property Selector and Where<T> Query using Linq
- How can I write the following code more elegantly using LINQ query syntax?
- How to group only subsequent items with the same property using linq
- Can I parameterize the property name of a PropertyExpression using LINQ Expressions?
- Select property by string name using LINQ
- LINQ query using list of objects property of an object
- Need Help Filtering A LINQ Query Using An Entity Framework Navigation Property
- Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate
- Linq sub query when using a repository pattern with EF code first
- Linq query - "Where" on List<T> Where reflected property Contains text/value
- Using LINQ query result for data source for GridControl c#
- LINQ Query using Group By
- LINQ query in C# using group and join
- Using Group in LINQ Query
- Custom Linq Query where property name is unknown at compile time
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How do I convert this tSQL statement to LINQ using group by in a sub query
- Group by year and month with given date range using Lambda or LINQ Query
- How to Select top (5) contributors group by Business type code in c# linq query
- Matching objects by property name and value using Linq
- EF6 using custom property in a linq query
- Linq query for two tables using SUM and Group By
More Query from same tag
- Need explanation about LINQ GroupJoin syntax
- Create XML using Linq to XML and arrays
- Dynamic LINQ expression for Select with child collection (Entity Framework)
- Expression for Linq between 2 classes in a 1 to many relationship
- Which methods to close a compiled query
- Calculate Account Receivables using LINQ
- Avoiding repeated projection code in Entity Framework
- LINQ EF Core left join
- Identity Filter Linq .Where
- Task Does Not Contain a Definition for Where If Done in One Line of Code
- how to access table from embedded where LINQ c#
- What is the best way to merge two objects during runtime using C#?
- List.Where( "string".Contains()) returns NULL in a LINQ Query
- Sort List<Object> with Duplicates by grabbing the one with the most recent date
- How to filter child collections Entity Framework
- Getting distinct and ordered members from a list of strings - linq or hashset for unique which one is faster / better suited
- Lambda Expression equivalent of "<>" in from clause of SQL with Join
- Easiest way to convert list to a comma separated string by a Specific Property?
- Code First calling a stored procedure passing in 3 params validation errors
- c# linq crm select where contains
- How to write to DB using Linq
- Using Linq for ObjectDataSource: How to transform datetime using ToShortTimeString?
- Finding out if an update succeeded with LINQ SQL?
- Select max as second criteria with Linq
- Using LINQ to isolate a particular object from a list of objects
- Expression Tree with Property Inheritance causes an argument exception
- Linq mystery error in EF query
- Unique ticket numbers for software support system
- Linq Loop Through Columns in Row C#
- Is it possible to get Directory list and filter by owner?