Methode Call twice inside LINQ Query

In our application we have a little query that looks like this:

var selectedAgents = from agent in listAgents where (this.collectionVehicles.GetVehicleByAgent(agent)).IsActive || (this.collectionVehicles.GetVehicleByAgent(agent)).IsSoldSinceCurrentSession select agent;

This works fine but the method GetVehicleByAgent is called two times and this is maybe not necessary.

Before LINQ we had a foreach-loop and did like this:

Vehicle parentVehicle = collectionVehicles.GetVehicleByAgent(agent); if (parentVehicle.IsActive || parentVehicle.IsSoldSinceCurrentSession) { selectedAgents.Add(agent); }

As you can see GetVehicleByAgent returns a Vehicle object and that can easiely compared more than once. So I am looking for a way the retieve the Vehicle inside the LINQ query and keep it. I don't want to call GetVehicleByAgent twice.

-------------Problems Reply------------

You need to use the let clause, like this:

var selectedAgents =
from agent in listAgents
let parentVehicle = collectionVehicles.GetVehicleByAgent(agent)
where parentVehicle.IsActive || parentVehicle.IsSoldSinceCurrentSession
select agent;

Category:linq Views:1 Time:2010-02-23

Related post

  • Using Custom Extension Methods Inside Linq Query 2010-10-06

    I have made my custom In extension method as shown below: public static class ExtensionMethods { public static bool In(this string str, IEnumerable<String> list) { foreach (var s in list) { if (s.Equals(str)) return true; } return false; } } An

  • Group method not recognized in LINQ query 2010-01-18

    LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore. I'm following this example: http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#minGrouped Here's my code: var doseWithLowestVolumeForForm = f

  • Using the lambda Include method in a compiled LINQ query 2011-11-16

    I'm currently trying to optimize some of the LINQ queries in my program by precompiling them. Some of these queries make extensive use of eager loading; here's an example of one: public static Func<DatabaseEntities, string, IQueryable<Employee

  • Regarding ArrayIndex inside Linq Query 2012-02-13

    i have LINQ expression node type ArrayIndex is not suported in LINQ to Entities error when i am trying to do the following public List<AttachmentList> ShowAttachments(int type, int ReferenceId) { try { var attachmentNames = (from attachment in

  • create some sort of loop inside linq query 2011-05-10

    I have a method that I want to use to filter a listview. I am creating the listview dynamically therfore I don't know the number of colums in advance. lstCurrentDynamicItems is a class with just one property and that property is named c and its type

  • What is the C# extension methods equivalent for this Linq query? 2010-11-22

    In this example: public void Linq40() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numberGroups = from n in numbers group n by n % 5 into g select new { Remainder = g.Key, Numbers = g }; foreach (var g in numberGroups) { Console.WriteLine(

  • How to optimize this LINQ query for Visual Studio? 2010-10-24

    I have this one gigantic complex LINQ to SQL query that I need to optimize somehow, because the background C# compiler completely hogs the CPU and I can't type or edit my .cs file normally in Visual Studio 2010 (every letter, especially if IntelliSen

  • The best way to build Dynamic LINQ query 2009-04-10

    Hi I am looking for best method for writing Dynamic LINQ query. I have a function like public IQueryable<Student> FindByAllStudents(int? id, string Name, int? CourseID, bool? IsActive) // like this way, all field values are passed { // code for

  • 'LINQ query plan' horribly inefficient but 'Query Analyser query plan' is perfect for same SQL! 2010-02-22

    I have a LINQ to SQL query that generates the following SQL : exec sp_executesql N'SELECT COUNT(*) AS [value] FROM [dbo].[SessionVisit] AS [t0] WHERE ([t0].[VisitedStore] = @p0) AND (NOT ([t0].[Bot] = 1)) AND ([t0].[SessionDate] > @p1)',N'@p0 int,

  • CRM 2011 LINQ query with multiple inner joins and a single WHERE clause (VB .NET) 2011-09-01

    I am having a great deal of difficuly trying to get LINQ queries working with CRM 2011. The problem occurs whenever I have three or more joined tables (doesn't seem to matter which CRM tables), and I try to add a WHERE clause (doesn't matter on which

  • Keep/Pass a Linq Query to a variable OOP 2011-11-22

    Is it Possible to keep/pass a like query into a variable in OOP Class for example: private somedatatype linqquery As i know declaring var type of variables can be done in a local-scope so how to achieve this ? --------------Solutions------------- If

  • does putting Linq query inside a method affect deferred excecution? 2011-04-23

    Linq query is not executed until the sequence returned by the query is actually iterated. I have a query that is used repeatedly, so I am going to encapuslate it inside a method. I'd like to know if it interferes with the deferred execution. If I enc

  • Calling a method inside a Linq query 2012-04-02

    I want to insert into my table a column named 'S' that will get some string value based on a value it gets from a table column. For example: for each ID (a.z) I want to gets it's string value stored in another table. The string value is returned from

  • Call class method inside the Linq Query 2010-11-03

    I have a method called GetAge(DateTime birthDay). I want to use this method in the Linq Query by passing the birthday and based on the returned age value need to perfrom some logic. I want below query in LINQ format - from customer in contetx.Custome

  • LINQ - Query syntax vs method chains & lambda 2011-11-07

    Does anyone stick to any rules (or are you forced to stick to any rules by your employer?) when choosing to use either LINQ query syntax or a Lambda expression inside one of the LINQ extension methods? This applies to any Entities, SQL, objects, anyt

  • Why I can't call a function inside a linq query? 2012-04-15

    This is my linq query (Linq to Sql) : pagina = (from Pagine page in kDataBase.Pagines where prova(page.title_it) == urlSuddivisa[i].ToLower() select page).FirstOrDefault(); and this is the function I call (inside the same class) : private string prov

  • What is the difference between LINQ query expressions and extension methods 2009-04-28

    Below are two queries that return the same data. Other then style I am not sure which is better. What factors influence these queries? What are the benefits of using one style over the other? Sample 1 var x = from s in db.Surveys join sq in db.Survey

  • C# Linq query Descendants method using variables 2009-09-29

    I am trying to use the C# Linq query Descendants method using variables. Here is part of the XML <CourseList> <course id="EM220" name="Fundamentals of Mechanics"/> <course id="EM305" name="Engineering Tools Seminar"/> <course id=

  • building method for LINQ query to strip chars from records 2009-10-14

    I need to strip the following chars from my records using LINQ to SQL query "\";:'.,[email protected]#$%^&*(_)~{}[]\\|<>? aeiouAEIOU" I can do it with my linq query here from p in customers select new {customer_name = String.Concat(p.First_name.Replace(

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.275 (s). 11 q(s)