this is the extension of my previous question if for any reason i have to join multiple tables( 7 to 8) and if use linq in an mvc app like this:
var data = (from a in context.GetModel()
join ad in aContext.GetModel() on a.CanId equals ad.CanId
join b in bContext.GetModel() on a.CanId equals b.CanId
join c in clContext.GetModel() on a.CanId equals b.CanId
join d in dContext.GetModel() on a.CanId equals d.CanId
where a.Id == id
select new
{
ac = ad,
ce = b,
skill = c,
exp = d
}).ToList() ;
so it proves that its a huge query which has a bulky out put as each join table has multple rows related to a candidate
i have to loop through data variable to fetch data from anonymous variables(ce,skill exp,....). for this i have to run 4 loops.
is there any other way to get these values without looping
OR
should i use db calls four times. what is more feasible or i must say best industry practices?
Tuhin PaulPosted Oct 12, 2024, 5:54 PM
declaring a Candidate type field within the Candidate model itself is not a good practice. By having a Candidate property within the Candidate model, you're creating a circular dependency. This can lead to serialization issues, infinite loops, and difficulties in mapping data.
Better approach, Remove the self-referential composition and flatten the data into separate properties within the Candidate model.
Create a separate view model that contains the required properties from the Candidate model and related entities.
umair mohsinPosted Oct 3, 2024, 6:32 PM
i am really thankful to all of you for sucha qucik response to my question.one more pain i wnt to give that i am sending this data to a view and my view is not ienumrable type.i declare these fields as iCollection in my candidate model and this model passed to view and i use this code:
var data = (from x in db.Candidates.Include("Academics").
Include("Certifications").
Include("Skills").
Include("Experiences")
where x.Id == id
select new
{
a = x.Skills,
b = x.Academics,
c = x.Experience,
d = x.Certifications,
e = x
}).ToList().Select(c => new Candidate
{
Academics = c.b,
Skills = c.a,
Experience = c.c,
Certifications = c.d,
profile=c.e
}).FirstOrDefault() ;
if i do not use second select statement in which there is a new Candidate{}, i can't get candidate table values which are stored in annonymous variable e i have debug this i got candidate table null . what i did in my candidate model i declare a field of candidate type and store candidate table values in it, then pass this whole var data to my view as this variable is of type candidate now so i can pass it to my view by doing this i got whole my candidate table values and other related values.
i want to ask that declaring candidate type field in a candidate model is ok or not.is thisa good practice.
Jignesh KumarPosted Oct 3, 2024, 7:02 AM
Hello Umair,
Please use Include to do eager loading and select only specific fields which you need
Jayraj ChhayaPosted Oct 3, 2024, 5:48 AM
In scenarios where you are joining multiple tables and retrieving a large dataset, as illustrated in your LINQ query, it is generally more efficient to minimize database calls. Instead of executing multiple database calls, which can lead to performance bottlenecks, consider leveraging the power of LINQ to project the necessary data in a single query.
If you find yourself needing to loop through the results to access specific properties, you can utilize LINQ's
SelectManyorGroupBymethods to flatten the data structure, reducing the need for nested loops.This approach allows you to access the required data without excessive looping. However, if the dataset is too large and performance is a concern, consider implementing pagination or filtering to limit the data returned from the database.
Anupam MaitiPosted Oct 2, 2024, 5:12 PM
IncludeandThenIncludeto automatically load related entities without having to explicitly join them (Look at the Example 1).Example 1:
Example 2 :
Tahir AnsariPosted Oct 2, 2024, 12:39 PM
Hi Umair,
You can use Navigation property.
Project into a DTO
The best approach depends on your specific use case, including how often you need the related data, the size of the data sets, and performance considerations. Using navigation properties and DTOs tends to be cleaner and often performs well.