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 Query
- 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
- How do you filter a collection of objects in a base class based on the type of the sub class created?
- 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
- Linq select array items based on sub class property
- LINQ searching an object sub property collection by just the id
- 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
- Linq: select parent based on sub collection
- 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?
- Optimal LINQ query to get a random sub collection - Shuffle
- 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
More Query from same tag
- LINQ to Entities / LINQ to SQL: switching from server (queryable) to client (enumerable) in the middle of a query comprehension?
- Non-keyed associations with Entity Framework
- Invalid Cast Exception DBQuery LINQ
- C# compare lists in a resource efficient way
- linq join on guid and string column
- How to use a copy-constructor in a LINQ to Entities query?
- LINQ - How can I give a child object in a select statement a reference to its parent?
- How to add an element as the n child to an element in LINQ
- Convert Expression<Func<XClass, object>> to Expression<Func<YClass, object>>
- how to invoke a method for each item using LINQ?
- Trouble translating small C# "Random String" function to Haskell
- LINQ query to use right key for the correct keyvaluepair
- MongoDb driver c# query optimization
- Linq statement : Order by
- How can I get the average of a property in a list using LINQ?
- Entity framework - select nullable join property
- Is it possible to change gridviewid to to a gridview DataSouce after the gridview is loaded
- Linq does not group in VB.Net
- Get element from a List<Class> of classes
- LINQ - Association mapping - Null reference on InsertOnSubmit
- Can I create a generic GroupBy() in C# LINQ?
- Order date ascending
- C# Lambda Expression for iterating through nested objects
- checking list contains string
- Update XAttribute Value where XAttribute Name = X
- Concatenate collection of XML tags to string with LINQ
- Dynamically generate Linq Select
- Copy Row in ClosedXML
- Linq Join on Two Conditions
- Best way to filter query by navigation property(child objects) in LINQ to SQL