logo
down
shadow

optimize linq query with related entities


optimize linq query with related entities

Content Index :


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Tag : linq , By : Lord Zantor
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , this may be a very basic question but I haven't used Linq much so do need help. I want to retrieve a list of records based on the related record count. , You can check by using Count() <= 1
Linq style:
from contact in context.ContactSet
where contact.Roles.Count() <= 1
select contact;
context.ContactSet.Where(c => c.Roles.Count() <= 1);

Is there any way to optimize this LINQ to Entities query?


Tag : chash , By : qba73
Date : March 29 2020, 07:55 AM
will help you Using a let to reduce the number of r.First()'s will probably improve performance. It's probably not enough yet.
 var q = from r in ctx.Responses
                .Where()
                .GroupBy()
     let response = r.First()
     orderby response.User.AwardCity, response.Category.Label, r.Count() descending
     select new
     {
         City = response.User.AwardCity,
         Category = response.Category.Label,
         Response = response.ResponseText,
         Votes = r.Count()
     };

Tag : linq , By : tjh0001
Date : March 29 2020, 07:55 AM
To fix the issue you can do This query is correct although its a cross-join - is simply the Cartesian product of two sets -.
You can explicitly specify a join based on equality condition.
var query = from par  in context.Parents
            join child  in context.Childrens on child.ParentID equals par.ID
            join pet  in context.Products on child.ID equals pet.ChildID
            Where par.parent_id = 1
            select new { par.Name, par.Id };

sqlite query slow, how to optimize (using linq to entities)


Tag : chash , By : Grace Jones
Date : March 29 2020, 07:55 AM
wish helps you The biggest optimization would be changing the Contains to a StartsWith. Which would be equivalent to changing from name like '%search%' to name like 'search%'. Otherwise SQLite can't fully use the indexes you placed on the columns and you are basically searching the whole table.
// First Name
if (!string.IsNullOrEmpty(firstName))
{
  firstName = firstName.Trim().ToUpper();
  records = records.Where(x => x.FirstName.StartsWith(firstName));
}

Tag : chash , By : yossi
Date : March 29 2020, 07:55 AM
Hope that helps I have two related tables with the following structure: , Starting over assuming everything in memory only...
var cured_receptions = new HashSet<int>((from r in receptions where r.ReceptionStart < endDateTime select r.PatientId));
var cured_patients = from p in patients where cured_receptions.Contains(p.Id) select p;
var rand = new Random();
var begin = DateTime.Now;
var receptions = Enumerable.Range(1, 100000).SelectMany(pid => Enumerable.Range(1, rand.Next(0, 100)).Select(rid => new { PatientId = pid, ReceptionStart = begin.AddDays(-rand.Next(1, 180)) })).ToList();
var patients = Enumerable.Range(1, 100000).Select(pid => new { Id = pid, Surname = string.Format("Smith{0}", pid) }).ToList();

var startTime = Util.ElapsedTime;
var endDateTime = new DateTime(2017, 5, 1);
//var cured_receptions = (from r in receptions where r.ReceptionStart < new DateTime(2017, 5, 1) select r.PatientId).Distinct().ToList();
//var cured_receptions = (from r in receptions where r.ReceptionStart < new DateTime(2017, 5, 1) select r.PatientId).Distinct();
//var cured_receptions = new HashSet<int>((from r in receptions where r.ReceptionStart < new DateTime(2017, 5, 1) select r.PatientId).Distinct());
//var cured_receptions = new HashSet<int>((from r in receptions where r.ReceptionStart < endDateTime select r.PatientId).Distinct());
//var cured_receptions = new HashSet<int>((from r in receptions where r.ReceptionStart < new DateTime(2017, 5, 1) select r.PatientId));
var cured_receptions = new HashSet<int>((from r in receptions where r.ReceptionStart < endDateTime select r.PatientId));
var cured_patients = from p in patients where cured_receptions.Contains(p.Id) select p;

//  var cured_patients = (from r in receptions
//                       where r.ReceptionStart < endDateTime
//                       join p in patients on r.PatientId equals p.Id
//                       select p).Distinct();

//  var cured_patients = from p in patients
//                       join r in receptions on p.Id equals r.PatientId into rj
//                       where rj.Any(r => r.ReceptionStart < endDateTime)
//                       select p;

cured_patients.Count().Dump();
var endTime = Util.ElapsedTime;

(endTime - startTime).Dump("Elapsed");
Related Posts Related QUESTIONS :
  • Extension interface patterns
  • Center text output from Graphics.DrawString()
  • Printing from a .NET Service
  • Capture MouseDown event for .NET TextBox
  • How do I generate WPF controls through code
  • Linq to objects - select first object
  • Visual Studio - new "default" property values for inherited controls
  • Anyone know a good workaround for the lack of an enum generic constraint?
  • Is the C# static constructor thread safe?
  • What is the difference between String and string in C#?
  • Multicore Text File Parsing
  • Invalid Resource File
  • Getting DirectoryNotFoundException when trying to Connect to Device with CoreCon API
  • How to wait for thread complete before continuing?
  • Is String.Format as efficient as StringBuilder
  • ASP.NET Web Service Results, Proxy Classes and Type Conversion
  • sgen.exe fails during build
  • In C#, why can't a List<string> object be stored in a List<object> variable
  • C# loop - break vs. continue
  • How to access .Net element on Master page from a Content page?
  • Why are unsigned int's not CLS compliant?
  • Why is Array.Length an int, and not an uint
  • How do I make event callbacks into my win forms thread safe?
  • Genealogy Tree Control
  • WCF Service - Backward compatibility issue
  • Binary patch-generation in C#
  • Tab Escape Character?
  • When do Request.Params and Request.Form differ?
  • The imported project "C:\Microsoft.CSharp.targets" was not found
  • Numeric Data Entry in WPF
  • Print a Winform/visual element
  • C# logic order and compiler behavior
  • When to use an extension method with lambda over LINQtoObjects to filter a collection?
  • How to make a button appear as if it is pressed?
  • C# and Arrow Keys
  • How do you resolve a domain name to an IP address with .NET/C#?
  • Should the folders in a solution match the namespace?
  • How can I evaluate C# code dynamically?
  • CSharpCodeProvider Compilation Performance
  • How can I create Prototype Methods (like JavaScript) in C#.Net?
  • DataTable Loop Performance Comparison
  • CSV string handling
  • What is the best way to do unit testing for ASP.NET 2.0 web pages?
  • High availability
  • What to use for Messaging with C#
  • Accessing a Dictionary.Keys Key through a numeric index
  • ConfigurationManager.AppSettings Performance Concerns
  • What Are Some Good .NET Profilers?
  • Is this a good way to determine OS Architecture?
  • How to create a tree-view preferences dialog type of interface in C#?
  • Searching directories for tons of files?
  • Can I have a method returning IEnumerator<T> and use it in a foreach loop?
  • Why can't I have abstract static methods in C#?
  • Displaying ad content from Respose.WriteFile()/ Response.ContentType
  • Convert integers to written numbers
  • Absolute path back to web-relative path
  • How can we generate getters and setters in Visual Studio?
  • Bringing Window to the Front in C# using Win32 API
  • Possible to "spin off" several GUI threads? (Not halting the system at Application.Run)
  • Reading a C/C++ data structure in C# from a byte array
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com