score:2
you could create a dictionary for the version comments and use it to simplify the population of the output
's comments
property.
the suggestion below is based on the assumption that for each version
's collection of records associated with the target userid
, there is:
- exactly one record for a change in the "comments" column
- at least one record for a change in any other column
i have marked all changes made to the original code with comments.
var query = context.userchangelogs
.where(u => u.userid == 100)
.orderbydescending(p => p.version); //latest version data first
.tolist();
// added
const string commentscolumn = "comments";
// added
// dictionary associating the version comment with the id of the first record for the version
var commentforrecordwithid = query
.groupby(entry => entry.version)
.todictionary(
gr => gr.first(entry => entry.column != commentscolumn).id,
gr => gr.first(entry => entry.column == commentscolumn).updatedvalue);
var output = query
.where(a => a.column != commentscolumn) // added - we don't want the comments column change records in output
.select(a => new userchangelogspoco
{
id = a.id,
version = a.version,
column = a.column,
originalvalue = a.originalvalue,
updatedvalue = a.updatedvalue,
// changed:
comments = commentforrecordwithid.containskey(a.id)
? commentforrecordwithid[a.id]
: null
}).tolist();
edit: the code above also assumes that the context.userchangelogs
is in chronological order, i.e. ordered by id
, at the time the query
variable is assigned.
Source: stackoverflow.com
Related Query
- Problem in populating list object in a certain way
- NUnit: What is the most concise way to assert whether an IEnumerable contains an object of a certain type?
- I am trying create a new list of users from a list of users who have not created an additional object with a certain property using LINQ
- Is there a way to transform a list of key/value pairs into a data transfer object
- Is there a concise way to determine if any object in a list is true?
- Optimized way to select items from a Collection excluding a list of certain member using Linq
- What is the best way to get the percentage for an object in linq list and map it to JSON?
- Linq to sql as object data source - designer problem with partial classes
- Elegant way to check if a list contains an object where one property is the same, and replace only if the date of another property is later
- LINQ - get all members of a certain type in a list of object
- Not able to sort list in a certain way using OrderBy C# functionality
- Cleanest way to check for consistency of multiple values in an object list
- What is the correct way of retrieving a given business object from a list of business objects using LINQ Where vs. Find?
- List of Dates ordered in a certain way
- C# List of different object types: Better way than Zip to check equality
- Linq code to get the index of an object in an array from an object within a list
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Is there a way to get to list a query of an object under a primary key id? the object itself has foreignkey referenced on the main table in aspnetcore
- How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?
- Better way to write this C# Code For List of Lists
- Is there a way to use an object from a list only if it exists?
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- List or Array of String Contain specific word in Html Source Code
- Is there a way to remove certain duplicates of a list but not others?
- LINQ: How to select entire list of list by certain object property value
- Efficient way to search and build list from a list(of string type) based on certain criteria - C#
- Why is my code returning a list of the same object 4 times?
- c# Linq or code to extract groups from a single list of source data
- Getting a nested object in C# from Json from a list by specifying a certain attribute
- Is there any way to get a JSON object as a list of view models and update records in database with single AJAX call?
More Query from same tag
- Linq assign values from one table into another
- How to get an object from an asynchronous method?
- Find Difference between end dates by comparing rows
- Array subtraction in PowerShell
- Implicit Cast not happening in Expression Tree
- SubSonic 3 Linq projecting anonymous types but not class types
- Group by and Sum for the list is the final aggregate result
- EFCore Linq Cannot perform an aggregate function on an expression containing an aggregate or a subquery
- EntityFrameworkCore GroupBy Select FirstOrDefault() SystemInvalidOperationException
- Linq sub list Problem
- C# Super fancy LINQiness
- Linq to entities prob after update to MVC5
- Entity Framework Insert In Code
- Doing a Cast Within a LINQ Query
- Best open source LINQ provider
- Dynamically add new lambda expressions to create a filter
- SQL statement to linq lambda
- Removing element(s) of the list of object in objects collection
- query that gets only the records needed to the page using Linq- not fetched the record based on page size
- Linq order by issue
- MongoDB .NET Driver Group By Time Range
- How do you order by Greatest number in linq
- How to more efficiently materialize related items using EF and LINQ
- Linq complex object groupBy
- storing Linq To Entities query in new object projection
- Simple nested repeater example using LINQ in C#
- Make a Search Method generic using LINQ
- Filtering folders from a list when using LINQ to sort to find the oldest file
- Flatten a Dictionary<int, List<object>>
- How can I find object in List with Linq?