rivantsov / vita

VITA Application Framework
MIT License
59 stars 15 forks source link

[Question] Need advice #197

Closed jasonlaw closed 3 years ago

jasonlaw commented 3 years ago

Hi @rivantsov ,

Will the multiple calls of queries as shown below will affect the Smart Loading? If it will, could you please advise how to combine the multiple queries into one? I am not sure how since there is no Or where clause in Linq.

public static List<IPoll> GetPollResults(OperationContext context)
{
    var localTime = DateTime.Now.ToClientTime();
    var community = context.Community();
    var member = context.Identity().Auth().User().Member;
    var unit = member.Unit;
    var polls = context.Session().EntitySet<IPoll>()
                       .Where(x => x.Community == community)
                       .Where(x => x.EndOn < localTime)
                       .Where(x => x.PollMode == PollMode.Open && x.Options.Any(o => o.Voters.Any(v => v.Member == member)))
                       .ToList();

    if ( unit != null)
    {
        var unitPolls = context.Session().EntitySet<IPoll>()
                               .Where(x => x.Community == community)
                               .Where(x => x.EndOn < localTime)
                               .Where(x => x.PollMode == PollMode.Unit && x.Options.Any(o => o.Voters.Any(v => v.Unit == unit)))
                               .ToList();

        polls = polls.Union(unitPolls).ToList();

        if ( unit.Owner == member)
        {
            var ownerPolls = context.Session().EntitySet<IPoll>()
                                    .Where(x => x.Community == community)
                                    .Where(x => x.EndOn < localTime)
                                    .Where(x => x.PollMode == PollMode.Owner && x.Options.Any(o => o.Voters.Any(v => v.Member == member)))
                                    .ToList();

            polls = polls.Union(unitPolls).ToList();
        }
    }

    return polls;
}

Thank you very much!

rivantsov commented 3 years ago

affect SmartLoading - none of the things like these... Smart Loading is in action AFTER you run the main query, have your entities and start going thru them doing biz logic, touching properties of entities - that's when SmartLoading kicks in. Having said that, I would suggest to double-check the SQLs produced by these Linq queries. I admit, the Linq engine in VITA is far from perfect and can produce invalid queries for non-trivial Linq expressions. So double-check the SQLs!

jasonlaw commented 3 years ago

Thanks for the clarification, I have better understanding on how SmartLoading is working now. :)