How to get the product count without using lambda expressions? - c#

I have a table products:
product_id
product_name
prodcut_price
My dbcontext name is abcentity.
I want to get how many number of products (like products count) along with product_id,product_name,product_price.
I have done using lambda expressions using Groupby product_id (this is for Linq to Sql, but I am using linq to entity).
I don't want to use lambda expressions. Is it possible to get the product count without using lambda expressions?

Are you just trying to avoid lambda syntax? You can use the group by clause in LINQ query expression syntax instead of calling .GroupBy().
from p in abcentity.products
group p by p.product_id into g
select new {g.Key, g.Count()}
But I should point out that this still gets compiled as a lambda expression. All you're changing is the syntax you use to represent it.

If you're using Linq to SQL by default, then no. The Expression that is created from making the lambda is what is used to determine the query to create.
If you want a custom expression, you can execute any SQL you define - Scott Gu blogged about how to do this here.
If that is not what you want to do, then perhaps you can explain why you don't want to use lambda expressions?

Related

What is the difference between Linq and EF syntax? [duplicate]

This question already has answers here:
Is it Linq or Lambda?
(2 answers)
Closed 5 years ago.
What is difference of
Linq: var data=from a in context.object select a;
EF: var data=context.object().Tolist();
They are both LINQ.
The first is query (expression) syntax
IEnumerable<int> numQuery1 =
from num in numbers
where num % 2 == 0
orderby num
select num;
And the other is method syntax
IEnumerable<int> numQuery2 = numbers
.Where(num => num % 2 == 0)
.OrderBy(n => n);
Source: MSDN: Query Syntax and Method Syntax in LINQ (C#)
In your case the difference is that in first case data will be of lazy IEnumerable type and and will be executed on enumerating it.
Second example will enumerate colletion to a list and collection will be in memory after execution of ToList method.
But I assume you are interested in difference between LINQ operators like from, where, select and method Where, Select, etc. There is not difference as operators are compiled to methods.
Difference between both syntax is:
Your 1st query is LINQ to Sql and 2nd one is Entity SQL.
Entity SQL is processed by the Entity Framework Object Services directly
Entity SQL returns ObjectQuery instead of IQueryable
You mean to know the kinds of difference in query syntax while Entity Framework.
Basics of LINQ & Lamda Expressions
LINQ
Linq is the Microsoft's first attempt to integrate queries into language. We know, it is really easy to find data from sql objects simply writing a query while its somewhat hectic when we want to do the same thing in a DataTable or Lists. Generally we will have to loop through every elements to find the exact match, if there is some aggregation we need to aggregate the values etc. Linq provides an easy way to write queries that can run with the in memory objects.
Lamda
A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions.
Hope it Helps.

Reuse LINQ's SELECT (When querying to tables with the same signature) [duplicate]

I have an Expression that converts one type of object to another type. The expression is as follows:
public Expression<Func<SQLRepository.ActionType, Model.ActionType>> DBActionTypeToActionType =
(SQLRepository.ActionType at) => new Model.ActionType()
{
ID = at.OID,
DisplayName = at.DisplayName
};
I can use the Expression like this:
var linq = (from at in dc.SQLRepositoryDC.ActionTypes select at).Select(DBActionTypeToActionType);
But I'd like to use it like this:
var linq = (from at in dc.SQLRepositoryDC.ActionTypes select DBActionTypeToActionType.Compile().Invoke(at));
I've been looking for a couple days now and I can only find references to doing this in the Where clause. It seems that if I'm able to use the function calls to do this, it should be possible using the query syntax.
The reason it is important to do use the query syntax is that some of the objects that are being selected are composed of many sub-objects and trying to chain them all of the conversions together with the function notation will be much harder to write and maintain.
It seems that if I'm able to use the function calls to do this, it should be possible using the query syntax.
That's not true. Query notation always goes via a lambda expression. For example
from x in y select z
ends up as
y.Select(x => z)
That means if you've already got an expression tree that you want to pass directly as the argument to Select, you can't use query expressions because there's this extra level of indirection.
Now the options available depend on where you need to apply the predefined expression. You can always use it in the source, and then continue with the query:
var query = from foo in dc.ActionTypes.Select(DBActionTypeToActionType)
where foo.Stuff
select foo.Other;
Or using it at the end is easy:
var query = (from bar in dc.ActionTypes
where bar.Stuff
select bar).Select(DBActionTypeToActionType);
Does that help at all?

How to substitute Expression<Func<T,bool>> into where clause of linq to sql expression

I would like to set the following predicate to where clause of linq statement written in expression syntax.
Expression<Func<Purchase, bool>> condition = p => p.Price > 100;
from purchase in dc.GetTable<Purchase>()
where condition
select ...
However, compiler cannot determine which Where to use: IQuaryable<> or IEnumerable<>.
How this problem could be solved without converting linq expression to method chains?
You can't do where condition just like that. Either you incorporate the condition in the where clause (where purchase.Price>100) or use the Where(condition) method call inside the query expression, like
from purchase in dc.GetTable<Purchase>().Where(condition)
select ...
This is the way you can combine them.
Try:
where condition.Compile()(purchase);

How to pass parameters to LINQ to XML query by using lambda expression?

I am new to LINQ to XML in .net(C#, asp.net). I want to know more about Lambda expressions. I am using Lambada expression with the following query.
count = FieldRoot.Element("USER").Element("MM")
.Descendants("MMMS")
.Where(a => a.Attribute("ID").Value == MID)
.SelectMany(b => b.Descendants("ABC")).Count();
Can you please tell me how the Lambda expression work specially in case of parameters (in the above case a & b) ? What is the basic concept of parameters in Lambda expression ? Can we use the names of the variables defined outside of the query instead of a & b ? which elements a & b represent in the above case represent ? Can we pass parameters from outside in the query ? If we can pass parameters from outside query then how it will be done? If you give me any other example instead of the above query to understand the concept of parameter in Lambda expression then it will also help me a lot.
The basic concept is that you're calling Where on a sequence of XElement values - so the lambda expression is executed several times, with a value for a as the "current" XElement. The lambda expression then says whether or not that XElement should be included in the results.
For the SelectMany call, this is effectively flattening a sequence of sequences - from one XElement, you're yielding a sequence of all the ABC descendant elements.
This is really just LINQ to Objects - it's just that LINQ to XML fits in very neatly with LINQ to Objects.

Reusing Expression in Linq select clause (query format)

I have an Expression that converts one type of object to another type. The expression is as follows:
public Expression<Func<SQLRepository.ActionType, Model.ActionType>> DBActionTypeToActionType =
(SQLRepository.ActionType at) => new Model.ActionType()
{
ID = at.OID,
DisplayName = at.DisplayName
};
I can use the Expression like this:
var linq = (from at in dc.SQLRepositoryDC.ActionTypes select at).Select(DBActionTypeToActionType);
But I'd like to use it like this:
var linq = (from at in dc.SQLRepositoryDC.ActionTypes select DBActionTypeToActionType.Compile().Invoke(at));
I've been looking for a couple days now and I can only find references to doing this in the Where clause. It seems that if I'm able to use the function calls to do this, it should be possible using the query syntax.
The reason it is important to do use the query syntax is that some of the objects that are being selected are composed of many sub-objects and trying to chain them all of the conversions together with the function notation will be much harder to write and maintain.
It seems that if I'm able to use the function calls to do this, it should be possible using the query syntax.
That's not true. Query notation always goes via a lambda expression. For example
from x in y select z
ends up as
y.Select(x => z)
That means if you've already got an expression tree that you want to pass directly as the argument to Select, you can't use query expressions because there's this extra level of indirection.
Now the options available depend on where you need to apply the predefined expression. You can always use it in the source, and then continue with the query:
var query = from foo in dc.ActionTypes.Select(DBActionTypeToActionType)
where foo.Stuff
select foo.Other;
Or using it at the end is easy:
var query = (from bar in dc.ActionTypes
where bar.Stuff
select bar).Select(DBActionTypeToActionType);
Does that help at all?

Categories

Resources