select sum(Sales) from WeeklyProductSale.
how i can write this query in linq and what this query will return to me.
thankas in advance.
VB.Net
Dim sumOfSales = Aggregate wps In db.WeeklyProductSale Into Sum(wps.Sales)
You can write it like this,
YourDBContextObject.WeeklyProductSales.Sum(w => w.Sales);
var answer = db.WeeklyProductSales.Sum (s => s.Sales);
This will return a variable of type s.Sales (e.g., decimal).
Edit: if you querying a DataTable, you will need to do this:
myDataTable.AsEnumerable().Sum (row => (decimal) row["Sales"]).
Make sure you reference System.Data.DataSetExtensions.dll and put "using System.Data.DataSetExtensions" into your program because the AsEnumerable extension method that this query uses relies on this. Change (decimal) to the type of the Sales column.
Related
I have this existing Sql statement:
Select Count(ordid),isnull(prcsts,'NOT STARTED')
from lwp
where lwp in( Select max(Id) from lwp group by ordid)
group by prcsts
I want to convert to use linq-to-sql, but I'm can't figure out how to handle the group by expression in the sub query. How can I do this?
I am using Entity Framework where I have a method to get the list of lwp. I did only part of it.
Entitydb.lwpmethod
.GetList
.Where(Function(F) F.ID = **Max(Function(O) O.ordid**)
.GroupBy(Function(F) F.prcsts)
.Select(Function(F) New With {.A = F.Count, .B = F.Key})
.ToList
I am unable to write the group by subquery in the max function.
First off, that's not an in, that's an = since max() returns a single element. Also your sql query has lwp in the where clause, you probably typo'd id. With that in mind, what you want is something like:
.Where(row=>row.ID=Entitydb.lwpmethod.GetList()
.Where(r=>r.ordid=row.ordid)
.Max(r=>r.ID))
C# code, but you get the idea.
By the way this looks like it's selecting the last row. Why not just sort by id descendently and take the first element?
I need help. I've some types defined:
Class1(){int ID; double Price;}
Class2(){int ID; Class1 myClass1;}
Class3(){int ID; List<Class2> Class2List;}
Now I have a list List<Class3> class3List, from which I need to take only the min double value (the min Price). Is this possible to do with LINQ to SQL, or do I need to use foreach loop?
var min = class3List.SelectMany(x => x.Class2List).Min(x => x.myClass1.Price);
Use SelectMany method to flatten your list of lists List<List<Class2>> into List<Class2>, and then return minimum value in a sequence of prices, fetched by simple selector x => x.myClass1.Price.
I have query:
var query = this.session.QueryOver<Products>()
.Where(uic => uic.PageNumber == nextPage[0])
.SingleOrDefault(uic => uic.ProductNumber)
But this query result is type Products. It is possible that result will be only integer type of column ProductNumber ?
Try something like this:
var query = this.session.QueryOver<Products>()
.Where(uic => uic.PageNumber == nextPage[0])
.Select(uic => uic.ProductNumber)
.SingleOrDefault<int>();
Since you need a single primitive type value, you can do .Select to define the result column, and then do .SingleOrDefault to get the only result. For complex types, you'd need to use transformers.
You can find more info about QueryOver in this blog post on nhibernate.info: http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html
You can use Miroslav's answer for QueryOver, but this would look cleaner with LINQ:
var productNumber = session.Query<Products>()
.Where(uic => uic.PageNumber == nextPage[0])
.Select(uic => uic.ProductNumber)
.SingleOrDefault();
Notice you don't need a cast, as the Select operator changes the expression type to the return type of its parameter (which is the type of ProductNumber).
I am trying to work out how to covert the script below from SQL in to LINQ. Any help would be welcome.
SELECT *
FROM
[tableName]
WHERE
[MyDate] IN
(SELECT
MAX([MyDate])
FROM
[tableName]
GROUP BY
[MyID])
I can't find an equivalent for the "IN" clause section. There are existing questions on this forum but none that cover selecting a DateTime.
Thanks in advance.
You can use the ".Contains(..)" function:
e.g.
var itemQuery = from cartItems in db.SalesOrderDetails
where cartItems.SalesOrderID == 75144
select cartItems.ProductID;
var myProducts = from p in db.Products
where itemQuery.Contains(p.ProductID)
select p;
Although it looks like 2 round trips, as the LINQ only constructs the query when the IEnumerable is tripped, you should get reasonable performance.
I think Any() is what you are looking for:
var result = tableName.Where(x =>
(from t in tableName
group t by t.MyID into g
where g.Max(y => y.MyDate) == x.MyDate
select 1).Any())
I am stuck with a problem here. I am trying to compare items in a list to another list with much more items using linq.
For example:
list 1: 10,15,20
list 2: 10,13,14,15,20,30,45,54,67,87
I should get TRUE if all the items in list 1 occur in list 2. So the example above should return TRUE
Like you can see I can't use sequenceEquals
Any ideas?
EDIT:
list2 is actually not a list it is a column in sql thas has following values:
<id>673</id><id>698</id><id>735</id><id>1118</id><id>1120</id><id>25353</id>.
in linq I did the following queries thanks to Jon Skeets help:
var query = from e in db
where e.taxonomy_parent_id == 722
select e.taxonomy_item_id;
query is IQueryable of longs at this moment
var query2 = from e in db
where query.Contains(e.taxonomy_item_id)
where !lsTaxIDstring.Except(e.taxonomy_ids.Replace("<id>", "")
.Replace("</id>", "")
.Split(',').ToList())
.Any()
select e.taxonomy_item_id;
But now I am getting the error Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.
How about:
if (!list1.Except(list2).Any())
That's about the simplest approach I can think of. You could explicitly create sets etc if you want:
HashSet<int> set2 = new HashSet<int>(list2);
if (!list1.Any(x => set2.Contains(x)))
but I'd expect that to pretty much be the implementation of Except anyway.
This should be what you want:
!list1.Except(list2).Any()
var result = list1.All(i => list2.Any(i2 => i2 == i));