I want to calculate a Sum value using fields from 2 tables and can't work out the syntax.
var x = (from l in context.Lessons
join t in context.Tariffs on l.TariffId equals t.Id
where l.StudentSemesterId == studentSemesterId
select new {
lessonCost = (t.Rate) * (l.Duration / 60)
});
This returns a set of values for individual lessons. How do I get the Sum total of these lessons?
You are selecting a new IEnumerable of an anonymous object type. Just select the correct value you are looking for. Then you have IEnumerable of float/double/decimal/whatever. Then take the resulting sum of that query.
var x = (from l in context.Lessons
join t in context.Tariffs on l.TariffId equals t.Id
where l.StudentSemesterId == studentSemesterId
select (t.Rate) * (l.Duration /60)).Sum();
Related
var CartItemsGroup = (from Cart in db.BuyOnlineCartMasters // orderby this Cart.CartID descending
join CartDetails in db.BuyOnlineCartDetails
on Cart.CartID equals CartDetails.CartID
join ProductMaster in db.tblBuyOnlineMasters.Where(x => x.CompanyID == CompanyID)
on CartDetails.BuyOnlineID equals ProductMaster.BuyOnlineID
//I tried here did not work , orderby Cart.CartID descending
select new { Cart.CartID, Cart.InvoiceNumber, ProductMaster.CompanyID,
ProductMaster.Price, ProductMaster.Title, ProductMaster.OfferPrice,
CartDetails.SoldPrice, CartDetails.Qty, CartDetails.ShippingStatus,
CartDetails.DeliveryStatus, TotalAmt = (CartDetails.Qty * CartDetails.SoldPrice)}
into x
//I tried here did not work, orderby x.CartID descending
//TODO: where conditions are not set for cart like payment paid etc
group x by x.CartID)
.ToList();
I want to order the result by the cartID , because of the groupby clause I am unable to get it done
The ordering needs to be after grouping.
To achieve your goal, replace
group x by x.CartID
with
group x by x.CartID into g
orderby g.Key descending
select g
First select your data then group by. That will work. A sample with my tables
from data in
(from o in DB.GM_ORDER
join g in DB.GM_ORDERITEMS on o.ORDERID equals g.ORDERID
join i in DB.GM_ITEM on g.ITEMID equals i.ITEMID
where o.ORDERID<=11160 /* or any other filter to be applied */
orderby o.ORDERID descending, i.ITEMCODE ascending /* in your case ORDERID will be CARTID. Continue sorting within the ORDER/CART with a second parameter (in your case can be delivery status) */
select new { o.ORDERID, i.ITEMCODE, g.ITEMID, Total = (g.CURR_PRICE * g.QTY) }
) select data
into x
group x by x.ORDERID
Below is my ERD and sample data. Note, I'm using Entity Framework and Code first to control my database.
For the project named Vacation, return all the DISTINCT users who have a "true" value in UserBooleanAttributes table for either the Parents or Teens rows defined in the UserAttributes table.
Here is my current attempt:
var myQuery =
from P in context.Projects
join UA in context.UserAttributes on P.ProjectID equals UA.ProjectID
join UBA in context.UserBooleanAttributes on UA.UserAttributeID equals UBA.UserAttributeID
join U in context.Users on UBA.UserID equals U.UserID
where P.ProjectID == 1
where UBA.Value == true
where (UA.UserAttributeID == 1 || UA.UserAttributeID == 2)
select new { uba = U };
This returns 6 users, with e#acme.org being listed twice. Is there a LINQ way of returning distinct values? I suppose I could convert this to a list then filter, but I'd rather have the Database do the work.
I'd rather avoid using lambda expressions if possible. Once again, I want the database to do the work, and not have to write code to union/intersect result groups.
i plan to join 2 table, and get the distinct value of language column. How should i achieve that in Linq? I try add 'group' but no luck. Besides, i want to select s value too together with r distinct language value.
My code:
public ActionResult QuestionLink(int Survey_ID)
{
var query = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model
on r.Qext_Question_ID equals s.Question_ID
where s.Question_Survey_ID == Survey_ID
group r.language << this is not work **
select r;
return PartialView(query.ToList());
}
This is what in MoreLinq is called DistinctBy. But if that method works on IEnumerable, so you can't use it in an EF query. But you can use the same approach:
var query = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model on r.Qext_Question_ID equals s.Question_ID
where s.Question_Survey_ID == Survey_ID
group new { r, s } by r.language into grp
select grp.FirstOrDefault();
But I wonder if this really is what you want. The result depends on the ordering of languages that the database happens to return. I think you should add a predicate for a specific language and remove the grouping:
var query = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model
on r.Qext_Question_ID equals s.Question_ID
where s.Question_Survey_ID == Survey_ID
&& r.language == someVariable
select new { r, s };
You can do like this:
var query = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model
on r.Qext_Question_ID equals s.Question_ID
where s.Question_Survey_ID == Survey_ID
group new {r, s} by r.language into rg
select rg.Key;
I wanting to know the best approach for a where clause with less than or equal to where the value to match is int?
var outOfStockProducts = (from theMapProd in context.tblProductOptions_MAP
join theProd in context.tblProducts on theMapProd.productID equals theProd.productID
where theProd.stock_Level <= 5
select theMapProd).ToList();
This is another way,
Not tested.
var outOfStockProducts = (from theMapProd in context.tblProductOptions_MAP
join theProd in context.tblProducts on theMapProd.productID equals theProd.productID
select theMapProd).ToList();
outOfStockProducts=outOfStockProducts.where(x=>x.stock_Level < 5 || x.stock_Level ==5).ToList();
I have the following view:-
CREATE VIEW tbl_adjudicator_result_view
AS
SELECT a.adjudicator_id, sar.section_adjudicator_role_id, s.section_id, sdr.section_dance_role_id, d.dance_id, c.contact_id,
ro.round_id, r.result_id, c.title, c.first_name, c.last_name, d.name, r.value, ro.type
FROM tbl_adjudicator a
INNER JOIN tbl_section_adjudicator_role sar on sar.section_adjudicator_role2adjudicator = a.adjudicator_id
INNER JOIN tbl_section s on sar.section_adjudicator_role2section = s.section_id
INNER JOIN tbl_section_dance_role sdr on sdr.section_dance_role2section = s.section_id
INNER JOIN tbl_dance d on sdr.section_dance_role2dance = d.dance_id
INNER JOIN tbl_contact c on a.adjudicator2contact = c.contact_id
INNER JOIN tbl_round ro on ro.round2section = s.section_id
LEFT OUTER JOIN tbl_result r on r.result2adjudicator = a.adjudicator_id AND r.result2dance = d.dance_id
When I run the following query directly against the db I get 0 in the count column where there is no result
select adjudicator_id, first_name, COUNT(result_id)
from tbl_adjudicator_result_view arv
where arv.round_id = 16
group by adjudicator_id, first_name
However when I use LINQ query I always get 1 in the Count Column
var query = from arv in db.AdjudicatorResultViews
where arv.round_id == id
group arv by new { arv.adjudicator_id, arv.first_name} into grp
select new AdjudicatorResultViewGroupedByDance
{
AdjudicatorId = grp.Key.adjudicator_id,
FirstName = grp.Key.first_name,
Count = grp.Select(p => p.result_id).Distinct().Count()
};
What do I need to change in the View / Linq query.
You're not doing the same thing in the LINQ query as in the SQL. COUNT(result_id) does not count distinct values of result_id - it counts non-null values.
Try this instead:
Count = grp.Select(p => p.result_id).Where(x => x != null).Count()
The point is: you're grouping your data in the LINQ query - and you'll always get at least one group.
That group's Count may be 0 - but the count of groups will be 1.