score:3
If you use the query syntax, you can declare variables.
This is how your query would look like in query syntax (please name your variables properly. I don't know what you are actually doing so I can't name them...):
var query = from a in UoW.Repository
.Get(echangeFilter)
group a by new
{
Id = MyBLL.IsInComing(a.idSens) ? MyBLL.FindByNoContactModel(a.change.idTo.GetValueOrDefault()).id : MyBLL.FindByNoContactModel(a.change.idFrom.GetValueOrDefault()).id,
Tri = MyBLL.IsInComing(a.idSens) ? MyBLL.FindByNoContactModel(a.change.idTo.GetValueOrDefault()).Tri: MyBLL.FindByNoContactModel(a.change.idFrom.GetValueOrDefault()).Tri,
SensAppel = a.echange_sens.nom
} into g
let b = new
{
group.Key.Id,
group.Key.Tri,
Count = group.Count(),
}
orderby g.Tri
select g;
var stats = query.ToList();
Now we can introduce a let
:
var query = from a in UoW.Repository
.Get(echangeFilter)
let x = MyBLL.IsInComing(a.idSens) ? MyBLL.FindByNoContactModel(a.change.idTo.GetValueOrDefault()) : MyBLL.FindByNoContactModel(a.change.idFrom.GetValueOrDefault())
group a by new
{
Id = x.id,
Tri = x.Tri,
SensAppel = a.echange_sens.nom
} into g
let b = new
{
g.Key.Id,
g.Key.Tri,
Count = g.Count(),
}
orderby g.Tri
select g;
var stats = query.ToList();
score:-1
You can combine the sub-expressions by pulling out the common elements and then using an intermediate anonymous class to hold them (I am not sure how EF 6 will handle this (what does Get
return?) as I don't know what MyBLL
is):
var stats = UoW.Repository
.Get(echangeFilter)
.Select(a => new {
a.echange_sens.nom,
fncm = MyBLL.FindByNoContactModel(
(MyBLL.IsInComing(a.idSens)
? a.change.idTo
: a.change.idFrom)
.GetValueOrDefault())
})
.GroupBy(nf => new {
Id = nf.fncm.id,
Tri = nf.fncm.Tri,
SensAppel = nf.nom
})
.Select(group => new {
group.Key.Id,
group.Key.Tri,
Count = group.Count(),
})
.OrderBy(g => g.Tri)
.ToList();
score:0
This works by first returning whatever model FindByNoContactModel returns per record and SensAppel, so you only have to call the IsIncoming and FindByNoContactModel once per row. Depending on how many rows you expect to be returned, you may find very quickly that you are better off pushing these lookups elsewhere, or letting the database do it.
var stats = UoW.Repository
.Get(echangeFilter)
.Select(a=> new {
Model = MyBLL.IsIncoming(a.idSens)
? MyBLL.FindByNoContactModel(a.change.idTo.GetValueOrDefault())
: MyBLL.FindByNoContactModel(a.change.idFrom.GetValueOrDefault()),
SensAppel = a.echange_sens.nom
})
.GroupBy(a => new
{
Id = a.Model.id,
Tri = a.Model.Tri,
SensAppel = a.SensAppel
})
.Select(group => new
{
group.Key.Id,
group.Key.Tri,
group.Key.SensAppel,
Count = group.Count(),
})
.OrderBy(g => g.Tri)
.ToList();
score:0
Is this what you're looking for?
var stats = UoW.Repository
.GroupBy(a => {
var repo = MyBLL.IsInComing(a.idSens) ? MyBLL.FindByNoContactModel(a.change.idTo.GetValueOrDefault()) : MyBLL.FindByNoContactModel(a.change.idFrom.GetValueOrDefault())
return new
{
Id = repo.id,
Tri = repo.Tri,
SensAppel = a.echange_sens.nom
}
})
.Select(group => new
{
group.Key.Id,
group.Key.Tri,
Count = group.Count(),
})
.OrderBy(g => g.Tri)
.ToList();
Source: stackoverflow.com
Related Query
- Linq : Use variable in a GroupBy
- How to assign LINQ Query to a variable and then use it later in the code
- Can I declare / use some variable in LINQ? Or can I write following LINQ clearer?
- How to use GroupBy using Dynamic LINQ
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- How can I switch that code to use LINQ
- Use one variable in multiple chained LINQ queries
- Can I use LINQ GroupBy to do this more cleanly?
- Problems trying to use GroupBy with multiple properties using the LINQ Method Syntax
- Use a linq query as microsoft local report Data Source (WinForms)
- LINQ Lambda efficiency of code groupby orderby
- LINQ: How to dynamically use an ORDER BY in linq but only if a variable is not string.empty or null
- creating Linq to sqlite dbml from DbLinq source code
- is it possible to use a range variable from a linq (query syntax) as a parameter?
- Use string variable in LINQ lambda expression
- how to use linq to get dictionary object's variable
- How to use linq functions like Sum() with dynamic variable
- In a Linq Expression body how to use the value of a variable instead of a reference to it?
- How can I use Linq expression for Join with GroupBy in EF Core 3.1
- How to use Join, GroupBy and Average on C# Objects using LINQ
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- How to use a string variable in the linq where clause?
- Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?
- use variable within a linq query
- Use variable value as field with LINQ
- source code for LINQ 101 samples
- Can I Use The Same Linq Code to Query for Different Conditions?
- C# LINQ join - use range variable in secondary join
- LINQ GroupBy Source = A Destination = B or Source = B and Destination = A
More Query from same tag
- c# linq: how to retrieve pieces of data from a returned record
- Dynamic linq backslash in where clause
- SQL Server TDE and LINQ
- Linq : Grouping rows and returning them in anonymous collections
- Succinct LINQ filter expression
- Sql Query with not in clause of subquery to LINQ query
- Grouping Linq Issue, can't get it right
- is there another way to do this query? (contains a split string in linq-to-sql query)
- Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.ParameterExpression
- EF Linq QUery cause lock compared to SQL
- Dynamic linq on Dynamic properties
- Many-to-Many Query in Entity Framework 6 without Junction Tables
- How to Convert Byte Array to Image inside in Linq output List
- 'Argument expression is not valid' when creating anonymous types dynamically
- Basic LINQ to SQL GridView binding
- How to include sorted navigation properties with Entity Framework
- C# LINQ INNER JOIN is returning wrong count
- Loading concrete child types based on abstract parent properties via LINQ
- add all items from _list1 that are not already present into _list2
- Entity Framework Query with multiple join conditions
- Querying serialized object file
- How to Join two IEnumerables with anonymous types?
- XElement - reading the nodes C#
- LINQ query throwing a cast exception
- How to build the correct LINQ query (generate OR instead of AND)
- Case Statements in LINQ query - AnonymousType to String?
- Problem in creating database through code first approach
- Linq search query using ||
- How To write an action for a partial view?
- When should I use IEnumerable and when IQueryable?