What's the better way to write this linq query? - c#

SQL:
SELECT node.CategoryId,
node.CategoryName,
node.Description,
node.Lft, node.Rgt,
node.ShowOnMenu,
(COUNT(parent.CategoryName) - 1) AS Level,
(CASE WHEN node.Lft = node.Rgt - 1 THEN 'TRUE' ELSE 'FALSE' END) AS Leaf
FROM Article_Category AS node,
Article_Category AS parent
WHERE node.Lft BETWEEN parent.Lft AND parent.Rgt
GROUP BY node.CategoryId,node.CategoryName,node.Description,node.Lft,node.Rgt,node.ShowOnMenu
ORDER BY node.Lft
My linq expression:
var list = (from node in DbContext.Categories
from parent in DbContext.Categories
where node.Lft >= parent.Lft && node.Lft <= parent.Rgt
select new
{
node.CategoryId,
node.CategoryName,
node.Description,
node.Lft,
node.Rgt,
node.ShowOnMenu,
ParentName = parent.CategoryName,
} into x
group x by new
{
x.CategoryId,
x.CategoryName,
x.Description,
x.Lft,
x.Rgt,
x.ShowOnMenu,
} into g
orderby g.Key.Lft
select new
{
CategoryId = g.Key.CategoryId,
CategoryName = g.Key.CategoryName,
Description = g.Key.Description,
Lft = g.Key.Lft,
Rgt = g.Key.Rgt,
ShowOnMenu = g.Key.ShowOnMenu,
Level = g.Count() - 1,
IsLeaf = g.Key.Lft == g.Key.Rgt - 1
}).ToList();
My question:
The linq expression is too long, there is two 'select new' expressions, i wonder how to make it shorter?
What's the corresponding Extension Method to the linq query? How can i express the "from... from...where..." with Extension method?

The first select new .. into x I don't see why you need, try removing it and write group node by new...
"from...from" is written as a lambda expression like this:
Categories.SelectMany(n => Categories, (n, p) => new { Node = n, Parent = p });

Related

Can anyone modify this LINQ to SQL query so it only returns one instance of each row?

var nodes = (from n in db.Nodes
join st in db.SessionTrackings on n.NodeID equals st.NodeID
where st.UserID == userid && st.GroupID == groupid
select n);
IDictionary<int, bool> trackingData = new Dictionary<int, bool>();
foreach (Node n in nodes)
{
trackingData.Add(new KeyValuePair<int, bool>(n.ID, true));
}
I keep getting a 'that key was already added' because there could be many SessionTrackings per Node, however I just want to get back all the Nodes that have at least 1 SessionTracking in existence for the NodeID but I don't need to get the Nodes more than once. If there are 4000 SessionTrackings for a Node (say ID = 45) I still only one 1 instance of Node 45 in my IQueryable. How can I modify my query for this? Please don't worry about why I need it in a Dictionary that's just the way it is.
You just need to tell the query engine that you only want distinct instances of your objects:
var nodes = (from n in db.Nodes
join st in db.SessionTrackings on n.NodeID equals st.NodeID
where st.UserID == userid && st.GroupID == groupid
select n).Distinct();
IDictionary<int, bool> trackingData = nodes.ToDictionary(n => n.ID, n => true);
If you don't need the nodes query for anything else, you can combine the statements like this:
IDictionary<int, bool> trackingData =
(from n in db.Nodes
join st in db.SessionTrackings on n.NodeID equals st.NodeID
where st.UserID == userid && st.GroupID == groupid
select n.Id)
.Distinct()
.ToDictionary(i => i, i => true);
This is how you can get the first session:
var nodes = (from n in db.Nodes
join st in db.SessionTrackings on n.NodeID equals st.NodeID
where st.UserID == userid && st.GroupID == groupid
select n)
.GroupBy(n => n.ID)
.Select(g => g.First());
I think you would need to do something like
var nodeids = (from n in db.Nodes
join st in db.SessionTrackings on n.NodeID equals st.NodeId
where st.UserId.equals(userid) && st.GroupID.equals(groupid)
select new {Id = n.Id})).distinct();
That should give you everything you need for the iteration, without repeats.

LINQ query - Multiple where with multiple sub queries based on int array

I have a sql query that performs the type of select I'm after:
select * from Products p where p.ProductId in (
select distinct ProductId from ProductFacets
where ProductId in (select ProductId from ProductFacets where FacetTypeId = 1)
and ProductId in (select ProductId from ProductFacets where FacetTypeId = 4)
)
There can be multiple FacetTypeIds passed into this query.
This query is constructed in a method based on a parameter argument of type int[].
public IEnumerable<Product> GetProductsByFacetTypes(string productTypeSysName, int[] facetTypeIds)
I'm trying to work out how to achieve this in LINQ. So far I've come up with something like this:
var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where p.FacetTypes.Any(x => x.FacetTypeId == 1)
where p.FacetTypes.Any(x => x.FacetTypeId == 4)
select p;
This returns the correct result set.
However I'm not sure how I can build this query using the int[] facetTypeIds parameter.
EDIT:
ProductFacets contains the following data:
ProductId, FacetTypeId
1, 1
1, 2
2, 1
2, 3
3, 4
3, 5
4, 1
4, 2
As an example, I'd like to be able to select only Products which have a FacetTypeId of 1 AND 2.
The result set should contain ProductIds 1 and 4
A local collection may be transmitted to the database by calling Contains:
from ft in facetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft;
The local collection is translated into sql parameters. Sql Server has a limit of ~2100 parameters, so beware.
Products that have any of the facets
from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where
(
from ft in p.FacetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft
).Any()
select p;
Products that have all facets.
int facetCount = facetTypeIds.Count();
from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where
(
from ft in p.FacetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft.FacetTypeId
).Distinct().Count() == facetCount
select p;
EDIT: Sorry I misread the question. I would suggest you use a PredicateBuilder and build up the Where clause dynamically if you need all of the types to be present. This would use extension methods directly.
var facetTypeIds = new [] { 1, 4, ... };
var predicate = PredicateBuilder.True<Product>();
foreach (int id in facetTypeIds)
{
int facetId = id; // avoid capturing loop variable
predicate = predicate.And( p => p.FacetTypes.Any( x => x.FacetTypeId == facetId );
}
var products = sc.Products
.Where( p => p.ProductType.SysName == productTypeSysName )
.Where( predicate );
Original (wrong, but left for context):
You want to use Contains. Note also you can use the logical and to replace multiple Where clauses with a single Where clause.
var facetTypeIds = new [] { 1, 4, ... };
var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
&& p.FacetTypes.Any( x => facetTypeIds.Contains( x.FacetTypeId ) )
select p;
This is based on tvanfosson's code. I have doubts about the performance of this approach though.
var facetTypeIds = new [] { 1, 4, ... };
var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
&& facetTypeIds.All(ft => p.FacetTypes.Any(x => x.FacetTypeId == ft))
select p;

LINQ to SQL in and not in

What is in and not in equals in LINQ to SQL?
For example
select * from table in ( ...)
and
select * from table not in (..)
What is equal to the above statement in LINQ to SQL?
You use, where <list>.Contains( <item> )
var myProducts = from p in db.Products
where productList.Contains(p.ProductID)
select p;
Or you can have a list predefined as such:
int[] ids = {1, 2, 3};
var query = from item in context.items
where ids.Contains( item.id )
select item;
For the 'NOT' case, just add the '!' operator before the 'Contains' statement.
I'm confused by your question. in and not in operate on fields in the query, yet you're not specifying a field in your example query. So it should be something like:
select * from table where fieldname in ('val1', 'val2')
or
select * from table where fieldname not in (1, 2)
The equivalent of those queries in LINQ to SQL would be something like this:
List<string> validValues = new List<string>() { "val1", "val2"};
var qry = from item in dataContext.TableName
where validValues.Contains(item.FieldName)
select item;
and this:
List<int> validValues = new List<int>() { 1, 2};
var qry = from item in dataContext.TableName
where !validValues.Contains(item.FieldName)
select item;
Please Try This For SQL Not IN
var v = from cs in context.Sal_Customer
join sag in context.Sal_SalesAgreement
on cs.CustomerCode equals sag.CustomerCode
where
!(
from cus in context.Sal_Customer
join
cfc in context.Sal_CollectionFromCustomers
on cus.CustomerCode equals cfc.CustomerCode
where cus.UnitCode == locationCode &&
cus.Status == Helper.Active &&
cfc.CollectionType == Helper.CollectionTypeDRF
select cus.CustomerCode
).Contains(cs.CustomerCode) &&
cs.UnitCode == locationCode &&
cs.Status == customerStatus &&
SqlFunctions.DateDiff("Month", sag.AgreementDate, drfaDate) < 36
select new CustomerDisasterRecoveryDetails
{
CustomerCode = cs.CustomerCode,
CustomerName = cs.CustomerName,
AgreementDate = sag.AgreementDate,
AgreementDuration = SqlFunctions.DateDiff("Month", sag.AgreementDate, drfaDate)
};
Please Try This For SQL IN
context.Sal_PackageOrItemCapacity.Where(c => c.ProjectCode == projectCode && c.Status == Helper.Active && c.CapacityFor.Contains(isForItemOrPackage)).ToList();

How is this Nested Set SQL query converted into a LINQ query?

Querying a Nested Set Model table, here's the SQL... how can this be written in LINQ?
SELECT parent.name
FROM nested_category AS node, nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'FLASH'
ORDER BY parent.lft;
particularly, the FROM part... never tried to do anything like that in LINQ.
Perhaps something like:
var query = from node in nested_category
from parentNode in nested_category
where node.lft >= parentNode.lft && node.rgt <= parentNode.rgt
&& node.name == "FLASH"
orderby parent.left
select parent.name;
if you do the LEFT OUTER join like that (that is without using into and DefaultIfEmpty()) then the top-level parent node will not be output (because it's parent is NULL)
My SQL version which goes:
SELECT
B.ELEMENT_CODE ParentElement,
A.ELEMENT_CODE ChildElement
FROM NESTED_SET_STRUCTURE AS A
LEFT OUTER JOIN NESTED_SET_STRUCTURE AS B
ON B.PROCEDURE = 'TEST1' AND B.FROM_LEFT = (SELECT MAX(C.FROM_LEFT)
FROM NESTED_SET_STRUCTURE AS C
WHERE C.PROCEDURE = 'TEST1' AND A.FROM_LEFT > C.FROM_LEFT
AND A.FROM_LEFT < C.TO_RIGHT)
WHERE
A.PROCEDURE = 'TEST1';
Translates as:
var nestedSets1 =
from a in NESTED_SET_STRUCTUREs
join b in NESTED_SET_STRUCTUREs on
new { a.PROCEDURE, FROM_LEFT = ((from c in NESTED_SET_STRUCTUREs
where c.PROCEDURE.Equals("TEST1")
&& a.FROM_LEFT > c.FROM_LEFT
&& a.FROM_LEFT < c.TO_RIGHT
select (c.FROM_LEFT)).Max()) } equals
new { b.PROCEDURE, b.FROM_LEFT } into bo
from bb in bo.DefaultIfEmpty()
where
a.PROCEDURE.Equals("TEST1")
orderby a.FROM_LEFT
select new {
ParentElement = bb.ELEMENT_CODE,
ChildElement = a.ELEMENT_CODE
};
Hope that helps, Paul.

Linq to SQL With Left Outer Join and Group By With Sum - How To

I'm trying to transform the SQL Query below into Linq to SQL
select Categorias.IdCategoria, Categorias.Nome, SUM(lancamentos.valor)
from lancamentos
left outer join Categorias on Lancamentos.IdCategoria = Categorias.IdCategoria
where Month(DataLancamento) = 11
and Credito = 1
and Lancamentos.Ocultar = 0
group by Categorias.IdCategoria, Categorias.Nome
This is what I've done
from lancamento in Lancamentos
where lancamento.Credito == true
&& lancamento.Ocultar == false
&& lancamento.DataLancamento.Month == 10
join categoria in Categorias on lancamento.IdCategoria equals categoria.IdCategoria into temp
from lancamentoJoinCategoria in temp.DefaultIfEmpty()
group lancamentoJoinCategoria by new { lancamentoJoinCategoria.IdCategoria, lancamentoJoinCategoria.Nome } into x
select new {
IdCategoria = (int?)x.Key.IdCategoria
, Nome = x.Key.Nome
}
How do I add the SUM(lancamentos.valor) to the linq to sql above ?
It will be:
(from lancamento in Lancamentos
join categoria in Categorias on lancamento.IdCategoria equals categoria.IdCategoria into temp
from lancamentoJoinCategoria in temp.DefaultIfEmpty()
where lancamento.Credito == true
&& lancamento.Ocultar == false
&& lancamento.DataLancamento.Month == 10
group lancamento by new { lancamentoJoinCategoria.IdCategoria, lancamentoJoinCategoria.Nome } into x
select new
{
IdCategoria = (int?)x.Key.IdCategoria,
Nome = x.Key.Nome,
sumValor = x.Sum(a=>a.valor)
});
You use the .Sum() method.
Eg;
Public Sub LinqToSqlCount03()
Dim q = (From o In db.Orders _
Select o.Freight).Sum()
Console.WriteLine(q)
End Sub
according to MSDN there is no query expression equivalent to the Sum() operation.
I provided a little sample how you could use the Method Syntax of Sum() in a query.
Some query operations, such as Count
or Max, have no equivalent query
expression clause and must therefore
be expressed as a method call. Method
syntax can be combined with query
syntax in various ways. For more
information, see LINQ Query Syntax versus Method Syntax (C#).
var example = new[]
{
new { Count = 1, Name = "a" }, new { Count = 2, Name = "b" },
new { Count = 2, Name = "c" }, new { Count = 2, Name = "c" }
};
var result = from x in example
select new
{
x.Name,
Sum = (from y in example
where y.Count.Equals(2)
&& y.Name==x.Name
select y.Count).Sum()
};
var distinct = result.Distinct().ToList();

Categories

Resources