Note that there are some explanatory texts on larger screens.

plurals
  1. POstreamline linq query
    primarykey
    data
    text
    <p>I have tried using Left outer join using linq. It gives me the same result anytime i change my report parameters.</p> <pre><code>var _result = (from ls in SessionHandler.CurrentContext.LennoxSurveyResponses from ml in SessionHandler.CurrentContext.MailingListEntries .Where(mle =&gt; mle.SurveyCode == ls.SurveyCode).DefaultIfEmpty() from lists in SessionHandler.CurrentContext.MailingLists .Where(m =&gt; m.MailingListId == ml.MailingListId).DefaultIfEmpty() from channel in SessionHandler.CurrentContext.Channels .Where(ch =&gt; ch.ChannelId == lists.ChannelId).DefaultIfEmpty() from tmChannelGroup in SessionHandler.CurrentContext.ChannelGroups .Where(tcg =&gt; tcg.ChannelGroupId == channel.ChannelGroupId).DefaultIfEmpty() from dmChannelGroup in SessionHandler.CurrentContext.ChannelGroups .Where(dcg =&gt; dcg.ChannelGroupId == tmChannelGroup.ParentChannelGroupId).DefaultIfEmpty() from amChannelGroup in SessionHandler.CurrentContext.ChannelGroups .Where(acg =&gt; acg.ChannelGroupId == dmChannelGroup.ParentChannelGroupId).DefaultIfEmpty() where (model.ChannelId != 0 &amp;&amp; channel.ChannelId == model.ChannelId || model.TMId != 0 &amp;&amp; channel.ChannelGroupId == model.TMId || model.DistrictId != 0 &amp;&amp; dmChannelGroup.ChannelGroupId == model.DistrictId || model.AreaId != 0 &amp;&amp; amChannelGroup.ChannelGroupId == model.AreaId || model.AreaId == 0 &amp;&amp; amChannelGroup.ChannelGroupId == model.LoggedChannelGroupId || model.DistrictId == 0 &amp;&amp; dmChannelGroup.ChannelGroupId == model.LoggedChannelGroupId || model.TMId == 0 &amp;&amp; tmChannelGroup.ChannelGroupId == model.LoggedChannelGroupId || model.ChannelId == 0 &amp;&amp; tmChannelGroup.ChannelGroupId == model.LoggedChannelGroupId) &amp;&amp; (ml.EmailDate != null || ml.LetterDate != null || ml.EmailBounce == null) select ls).ToList(); </code></pre> <p>I have this LINQ query which is repeated (sort of) based on the model value. How would I be able to shorten this query..if I could just use one var object instead of using a bunch for different parameters..as you see this code is repeated.</p> <pre><code>if (model.ChannelId != 0) { var _result = (from ls in SessionHandler.CurrentContext.LennoxSurveyResponses join ml in SessionHandler.CurrentContext.MailingListEntries on ls.SurveyCode equals ml.SurveyCode join m in SessionHandler.CurrentContext.MailingLists on ml.MailingListId equals m.MailingListId join ch in SessionHandler.CurrentContext.Channels on m.ChannelId equals ch.ChannelId where ch.ChannelId == model.ChannelId &amp;&amp; ml.EmailBounce == null || ml.EmailBounce.Equals(false) select ls).ToList(); var _SentSurveys = (from ml in SessionHandler.CurrentContext.MailingListEntries join m in SessionHandler.CurrentContext.MailingLists on ml.MailingListId equals m.MailingListId join ch in SessionHandler.CurrentContext.Channels on m.ChannelId equals ch.ChannelId where ch.ChannelId == model.ChannelId &amp;&amp; (ml.EmailDate != null || ml.LetterDate != null || ml.EmailBounce == null) select ml).ToList(); model.SentSurveys = _SentSurveys.Count() &gt; 0 ? _SentSurveys.Count() : 0; model.CompletedSurveys = _result.Count() &gt; 0 ? _result.Count() : 0; model.PercentageComplete = model.SentSurveys != 0 ? model.CompletedSurveys / model.SentSurveys : 0; //model.Referring = _result.Average(m =&gt; Convert.ToInt32(m.Question1Answer)); model.Referring = Math.Round(_result.Select(m =&gt; string.IsNullOrEmpty(m.Question1Answer) ? 0 : Double.Parse(m.Question1Answer)).Average()); model.ServicePerformance = Math.Round(_result.Select(m =&gt; string.IsNullOrEmpty(m.Question2Answer) ? 0 : Double.Parse(m.Question2Answer)).Average()); model.InstallPerformance = Math.Round(_result.Select(m =&gt; string.IsNullOrEmpty(m.Question3Answer) ? 0 : Double.Parse(m.Question3Answer)).Average()); model.ReferringLennox = Math.Round(_result.Select(m =&gt; string.IsNullOrEmpty(m.Question4Answer) ? 0 : Double.Parse(m.Question4Answer)).Average()); } else if (model.TMId != 0) { var _result = (from ls in SessionHandler.CurrentContext.LennoxSurveyResponses join ml in SessionHandler.CurrentContext.MailingListEntries on ls.SurveyCode equals ml.SurveyCode join m in SessionHandler.CurrentContext.MailingLists on ml.MailingListId equals m.MailingListId join ch in SessionHandler.CurrentContext.Channels on m.ChannelId equals ch.ChannelId where ch.ChannelGroupId == model.TMId select ls).ToList(); var _SentSurveys = (from ml in SessionHandler.CurrentContext.MailingListEntries join m in SessionHandler.CurrentContext.MailingLists on ml.MailingListId equals m.MailingListId join ch in SessionHandler.CurrentContext.Channels on m.ChannelId equals ch.ChannelId where ch.ChannelGroupId == model.TMId &amp;&amp; (ml.EmailDate != null || ml.LetterDate != null || ml.EmailBounce == null) select ml).ToList(); model.SentSurveys = _SentSurveys.Count() &gt; 0 ? _SentSurveys.Count() : 0; model.CompletedSurveys = _result.Count() &gt; 0 ? _result.Count() : 0; model.PercentageComplete = model.SentSurveys != 0 ? model.CompletedSurveys / model.SentSurveys : 0; model.Referring = _result.Select(m =&gt; string.IsNullOrEmpty(m.Question1Answer) ? 0 : Double.Parse(m.Question1Answer)).Average(); model.ServicePerformance = _result.Select(m =&gt; string.IsNullOrEmpty(m.Question2Answer) ? 0 : Double.Parse(m.Question2Answer)).Average(); model.InstallPerformance = _result.Select(m =&gt; string.IsNullOrEmpty(m.Question3Answer) ? 0 : Double.Parse(m.Question3Answer)).Average(); model.ReferringLennox = _result.Select(m =&gt; string.IsNullOrEmpty(m.Question4Answer) ? 0 : Double.Parse(m.Question4Answer)).Average(); } </code></pre> <p>and there are 5 additional model parameters and for each parameters, a new var _result and _SentSurveys are created..i just want to streamline this code. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload