Posts

Showing posts with the label linq

C# Linq that includes sorting and grouping

Image
Clash Royale CLAN TAG #URR8PPP C# Linq that includes sorting and grouping This is my code: public class Photos { public long PhotoLabel { get; set; } public int UserID { get; set; } } List<Photos> photolist = new List<Photos>(); var result1 = photolist.OrderByDescending(p => p.PhotoLabel).ThenBy(r => r.UserID).ToList(); If I display the contents now, this is what I get (First sorted in descending order of PhotoLabel and then sorted by UserID: |------|---------------------|---------------------| | Row | UserID | PhotoLabel | |----------------------------|---------------------| | 1 | 92 | 20180729181046 | |----------------------------|---------------------| | 2 | 92 | 20180729181041 | |----------------------------|---------------------| | 3 | 92 | 20180729181037 | |----------------------------|---------------------| | 4 | 88 | 20180729174...

T-SQL to LINQ to SQL using Navigation Properties

Image
Clash Royale CLAN TAG #URR8PPP T-SQL to LINQ to SQL using Navigation Properties I can’t seem to come up with the right corresponding LINQ to SQL statement to generate the following T-SQL. Essentially, I'm trying to return payment information with only one of the customer's addresses... the AR address, if it exists, then the primary address, if it exists, then any address. SELECT < payment and address columns > FROM Payment AS p INNER JOIN Customer AS c ON c.CustomerID = p.CustomerID OUTER APPLY ( SELECT TOP 1 < address columns > FROM Address AS a WHERE a.person_id = c.PersonID ORDER BY CASE WHEN a.BusinessType = 'AR' THEN 0 ELSE 1 END , a.IsPrimary DESC END ) AS pa WHERE p.Posted = 1 We’re usin...

Get the less-sold items with LINQ

Get the less-sold items with LINQ I have 2 objects (Order and Product) and a third (OrderDetail) that will be used as "navigation" between Products and Orders. I'm trying to build a view that will show the less-sold products. For that I am "querying" the object OrderDetail and saving the result in a view model to later on be used in the view. Model: public class Product { public int ProductID { get; set; } public string CodProduct { get; set; } public string Nome { get; set; } (...) public ICollection<OrderDetail> OrderDetails { get; set; } } public class Order { public int OrderID { get; set; } (...) [BindNever] public ICollection<OrderDetail> OrderDetails { get; set; } } public class OrderDetail { public int OrderDetailId { get; set; } public int OrderId { get; set; } public int ProductID { get; set; } public int Quantity { get; set; } public decimal UnitPrice { get; set; } public virtual Product Product { get; set; } ...