c# - Disposing the result of a LINQ query -
In a code that is used to check the existence of an object based on a condition, How do I calculate the query store after being counted?
var in query = someContext where p (p.Name == name and p.Status == true) select p; If (query.Count ()> gt;) true truth back; Second false return; This is a simple scenario, but there can be a situation where there may be a large collection of queries, so my question is in those cases where the requirement is only to check the existence of the object How can this query be written so that the result is not stored in the archive, after which the count is checked.
Regards.
What is the LINQ extension method .ount () actually uses In the case of the unit framework (assuming what you are using), a SQL query will be generated which will return the record count, not record itself. Thus, you do not have to worry about disposal of a large collection. Example code:
class program {static void main (string [] args) {using (var db = New TestContext ()) {db.Database.Log + = Console.WriteLine; Var query = from MF in DB Manufacturer where mfg.Name.Contains ("Ink") select mfg; Var calculation = query.Count (); } Console. Readline (); }} Generates SQL:
Select [GroupBy1]. [A1] From AS [C1] (SELECT COUNT (1) AS [A1] FROM [DBO]. Producer] AS [Extent 1] where [ext. 1]. [Name] like 'N%%' ) AS [Group B1]
Comments
Post a Comment