How to get parent model as a result. I want to get MasterData type as a result. i want to use where condition in Attributes but i want to receive Master Data Type as a result
var result= MasterData.SelectMany(ca => ca.Attributes)
.Where(x => MasterDataAttId_All.Contains(x.AttributeId))
.ToList();
Loading

Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 27, 2024, 9:45 AM EST
Hi Mert AKYILDIZ,
To achieve the desired outcome of retrieving the
MasterDatatype while applying a condition on itsAttributes, you can utilize theWhereclause in conjunction withAnyto filter the parent model based on the attributes.var result = MasterData .Where(md => md.Attributes.Any(attr => MasterDataAttId_All.Contains(attr.AttributeId))) .ToList();Jignesh KumarRank #91 · 22.9K reputationPosted Nov 28, 2024, 1:25 AM EST
Hello Mert,
Please use below query which will help you out,
var result = MasterData .Where(ca => ca.Attributes .Any(x => MasterDataAttId_All.Contains(x.AttributeId))) .ToList();Sangeetha SRank #212 · 11.4K reputationPosted Nov 27, 2024, 11:24 PM EST
To retrieving the parent model (Master Data type) while applying a filter condition on the child attributes, you can use a combination of
Where,Select, and possiblyDistinctif needed.Instead of using
SelectMany, you want to filter theMasterDatabased on the condition of its attributes and then return theMasterDataobjects themselves. Here’s how you can do that:var result = MasterData .Where(ca => ca.Attributes.Any(x => MasterDataAttId_All.Contains(x.AttributeId))) .ToList();Where: This filters theMasterDatacollection.Any: This checks if any of theAttributesin eachMasterDataobject satisfy the condition (i.e., if theirAttributeIdis contained inMasterDataAttId_All).ToList(): This converts the filtered result back into a list ofMasterDataobjects.