score:0
you can use following code:
var count = rangedata.select(x => new { id = x.item1?.port?.id ?? x.item2?.port?.id ?? 0, item = x })
.count(x =>
{
int? cdate = null; // change int to your desired type over here
if (x.id > 0 && data.trygetvalue(x.id, out cdat))
{
// do some operation. (var cdata = sumdata(x.id, x.item.item2.port.date)
return true;
}
return false;
});
edit:
@d stanley is completely right, linq is wrong tool over here. you can refactor few bits of your code though:
var date1 = rangedata.tolist();
int record =0;
foreach (var tr in date1)
{
int? cdat = null; // change int to your desired type over here
int id = tr.item1?.port?.id ?? tr.item2?.port?.id ?? 0;
if (id >0 && data.trygetvalue(id, out cdat))
{
// do some operation. (var cdata = sumdata(id, tr.item2.port.date)
record ++;
}
}
score:0
linq is not the right tool here. linq is for converting or querying a collection. you are looping over a collection and "doing some operation". depending on what that operation is, trying to shoehorn it into a linq statement will be harder to understand to an outside reader, difficult to debug, and hard to maintain.
there is absolutely nothing wrong with the loop that you have. as you can tell from the other answers, it's difficult to wedge all of the information you have into a "single-line" statement just to use linq.
score:2
i think your code example is false, your record variable is initialized to 0 on each loop so increment it is useless .
i suppose that you want to count records in your list which have an id, you can achieve this with one single count()
:
var record = date1.count(o => (o.item1?.port?.id ?? o.item2?.port?.id) > 0);
Source: stackoverflow.com
Related Query
- Code Rewite for tuple and if else statements by using LINQ
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Best practices for dealing with LINQ statements that result in empty sequences and the like?
- How to reuse a linq expression for 'Where' when using multiple source tables
- How can I code an outer join using LINQ and EF6?
- What are the prerequisites for using SQL and LINQ with my winforms application in C#?
- For each using linq how to use else
- BestPractice: Pros and Cons for using AutoMapper or LINQ (LINQ to Objects) for mapping between a Domain Model and a Presentation Model
- Using C# LINQ Expressions for both Value Types and Reference Types
- Finding common columns from two datatable and using those for Join condition in LINQ
- How to work around the fact that Linq to SQL is using separate SQL DELETE statements for each item in DeleteAllOnSubmit
- How to group by and sum for every 7 days using linq lambda?
- Stubbing Code for Test With Linq Expressions and Lambdas
- Get Min and Max for items from database using Linq
- Using LINQ query result for data source for GridControl c#
- Using LINQ to query/subquery multiple methods (Quartz.NET example for groups, jobs, and triggers)
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Using LINQ for Group and Sum
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- Nested sorting for predefined filters using LINQ and RavenDB
- Using LINQ to search two lists for match and return boolean for every item
- Algorithm/pattern for selecting sub-collections using LINQ and C#
- Linq query for two tables using SUM and Group By
- source code for LINQ 101 samples
- Computational and stylistic optimal solution for the conditional where clause using Linq
- Using LINQ and Reflection: How to query for all Classes with [Authorize] Attribute in my Assembly?
- LINQ query for Sum and view using View Model Class
- Is there a Linq Entity Framework query for using an inner join AND an IN() statement?
- Searching for multiple strings using single database query with entity framework and LINQ
- C# LINQ code for two list compare and replace
More Query from same tag
- Linq Union do not work
- Expression for Linq between 2 classes in a 1 to many relationship
- XML to Linq getting child nodes with attribute value
- ef 4.1 LINQ filter an element into a collection
- How can I get an entity with linq-query that is not in context?
- Linq GROUP & SUM the same columns from different tables
- How to increment column with last column value in Lambda or Linq
- "Cannot access non-static property", but making the field static or instantiating it brakes the code
- How to update a particular field of a particular object in Generic list in c#.net using LINQ
- Problem with Linq Left Join
- Linq 2 sql complex query (Count Issue)
- The left-hand side of an assignment must be a variable property or indexer
- How do you check if Record exists
- Implement a "join" with a variety of tables in Entity Framework
- Modifying XDocument Query with Variables
- How to use DbGeography.Filter in Linq with Entity Framework 5?
- In Linq-to-SQL, does IMultipleResults hit the database more than once?
- Linq to SQL intellisense has no knowledge of tables within the datacontext object
- Single enumeration with multiple filters
- Fluent LINQ query with multiple join conditions, one of which being a simple equals and another a less than comparison between ids
- How to project filtered 2nd level child objects in Linq to Entities?
- Linq "Sequence operators not supported for type" Exception
- LinQ does not recognize the method 'int32 toint32'
- How to group a list of data into year, season, month, week, day?
- Linq. Anonymous type error when joining to multiple tables
- C# LINQ Include
- Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'
- LINQ - recursive request
- How to copy all properties of an object to another in LINQ
- Error in LINQ Left JOIN