score:1
Accepted answer
You can define a method, and call it during the projection.
Note: Changed Assessment.Assessment property to Assessment.AssessmentDescription so the property name would not be the same as the enclosing type
public List<SummaryReport> CreateReport()
{
return Get()
.SelectMany(rev => rev.Assessments, (rev, ass) => new { rev, ass })
.Select(x => new SummaryReport
{
ReviewID = x.rev.ReviewID,
ReviewerID = x.ass.Reviewer.ReviewerID,
Assessment = x.ass.Assessment,
ReviewGroupID = x.ass.Reviewer.ReviewGroup.ReviewGroupID,
GroupAssessment = DeriveGroupAssessment(x.rev.Assessments)
})
.ToList();
}
private static string DeriveGroupAssessment(IEnumerable<Assessment> assessments)
{
string assessment = null;
if (assessments.Any(a => a.AssessmentDescription == "Big Problem"))
{
assessment = "Big Problem";
}
else if (assessments.All(a => a.AssessmentDescription == "No Problem"))
{
assessment = "No Problem";
}
// If you want the value to be null if neither of the above conditions are met, this logic is not needed
////if (!assessments.Any(a => a.AssessmentDescription == "Big Problem")
//// && assessments.Any(a => a.AssessmentDescription == string.Empty))
////{
//// assessment = null;
////}
return assessment;
}
score:1
Try,
public static List<SummaryReport> CreateReport()
{
return Get()
.SelectMany(rev => rev.Assessments, (rev, ass) => new { rev, ass })
.Select(x => new SummaryReport
{
ReviewID = x.rev.ReviewID,
ReviewerID = x.ass.Reviewer.ReviewerID,
Assessment = x.ass.AssessmentStr,
ReviewGroupID = x.ass.Reviewer.ReviewGroup.ReviewGroupID,
GroupAssessment = x.rev.Assessments.Any(a => a.AssessmentStr == "Big Problem") ? "Big Problem" :
x.rev.Assessments.All(a => a.AssessmentStr == "No Problem")? "No Problem" : null
})
.ToList();
}
Changed name of Assessment property to AssessmentStr because C# not allow property name same as class name.
Source: stackoverflow.com
Related Articles
- Summarize a property based on a sub collection
- How to sort a collection based on a subcollection property
- Is there any elegant way in LINQ to bucket a collection into a set of lists based on a property
- Find an item from collection based on the minimum value of a property
- Insert objects into a collection with LINQ based on a property of the existing objects in the collection
- Convert a Model Class Collection to Dictionary based on property as KEY using LINQ C#
- Dynamic linq to Dataset datarow based on collection property name
- Filter List collection based on property of child list contains a name
- LINQ - Select anonymous type which based on a collection property
- Flatten a collection based on an internal property
- Group outer collection based on a property value of nested (inner) collection
- How do I order a collection based on a child property using LINQ or a Lambda?
- Get a collection based on the property of an other collection with Linq
- OrderBy Collection in Dictionary Value based on Property of the Collection
- Linq how to query a list of items for a combined list of a child collection based on a property of the parent item
- Splitting collection of objects based on object property and threshold
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- C# - code to order by a property using the property name as a string
- Add items to a collection if the collection does NOT already contain it by comparing a property of the items?
- Group List of Objects based on Property using Linq?
- Query and updating a property in a collection using LINQ
- How to join two Lists based on common property
- ef-core load collection property of nested tph inherited member
- LINQ: grouping based on property in sublist
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- LINQ Contains Based on Property
- Building an OrderBy Lambda expression based on child entity's property
- Find all items whose collection property contains items in another list
- Sort a collection and rank the result based on certain criteria
- How to sort a collection based on type in LINQ
- how to use group by function for DbFunctions.DiffMinutes
- Member access not legal on type LINQ error
- Using LINQ to find all keys from one collection that are not in another?
- C# LINQ Group and sum List<T> then Group and sum another list within the first List
- Error while converting nullable datetime to string in LINQ asp.net MVC
- Turning IEnumerable of struct with inner IEnumerable into single dictionary
- Linq issue when using GUID
- Using LINQ to update every field in SQL row without specifically declaring each field?
- Call Length Property on Returned Array in Chained String/LINQ Methods of C#
- Remove an Item from C# Enum List
- Azure Table Storage Query in .NET With Property Names Unknown at Design Time
- reading data from multiple excel sheets with linq to excel (http://code.google.com/p/linqtoexcel/)
- LINQ - return all rows for a given ID if it contains xyz
- Entity framework `AsNoTracking` is not working with anonymous projection
- What's the benefit of .Cast over .Select?
- LINQ ThenBy in circle
- Returning IEnumerable<T> vs. IQueryable<T>
- Does WCF suppress first-chance exceptions?
- Call is failing with null parameter
- Find Minimum and Maximum of a column in LInq query result