Get last record in linq c#
WebMy Linq query returns one record for each company, but it doesn't return the most recent ones: var query = from p in db.Payments where p.Status == false && DateTime.Compare (DateTime.Now, p.NextPaymentDate.Value) == 1 group p by p.CompanyID into op select op.OrderByDescending (nd => nd.NextPaymentDate.Value).FirstOrDefault (); WebI have a LINQ query to find records for the last 7 days which works fine No, you have a query that returns the calories burned more than 7 days ago (ie 8, 9, 10, etc). The way you structured your query, the correct way would be w.Date >= DateTime.Now.AddDays (-n) to get the last n days. Share Improve this answer Follow
Get last record in linq c#
Did you know?
WebLINQ to Entities: Get more recent date from each group. UWP setting a ComboBox's SelectedItem when the ItemSource is LINQ. Analyzing queries by Entity Framework. Order by repeated times LINQ ASP.NET MVC. Linq query to get person visited zones of … WebDec 16, 2011 · LINQ var lastFiveProducts = (from p in products orderby p.ProductDate descending select p).Take (5); Lambda var lastFiveProducts = products.OrderByDescending (p => p.ProductDate).Take (5); Which ever you prefer. Share Improve this answer Follow answered Dec 16, 2011 at 10:23 James 79.7k 18 163 236
WebSep 22, 2008 · When I enter an object into the DB with Linq-to-SQL can I get the id that I just inserted without making another db call? I am assuming this is pretty easy, I just don't know how. ... c#.net; linq; linq-to-sql; ... Linq get last ID inserted. 1. F# obtaining ID after SubmitChanges (using SQLDataConnection type provider) ... WebAug 24, 2024 · You only want the first one when ordered by the Id in descending order. [HttpGet] public int GetStudentId () { var latestStudentRecord = _context.StudentModel .OrderByDescending (a => a.Id) .First (); // return only the Id property of the latest record return latestStudentRecord.Id; }
Web6 Answers. Sorted by: 248. If you just want the last date for each account, you'd use this: var q = from n in table group n by n.AccountId into g select new {AccountId = g.Key, Date = g.Max (t=>t.Date)}; If you want the whole record: var q = from n in table group n by n.AccountId into g select g.OrderByDescending (t=>t.Date).FirstOrDefault (); WebJan 4, 2012 · I want to get the second last of the the split string. So my output looks like this: null, 102, 1532, 45, 45 I have a solution for it that looks like this: ... c#; linq; linq-to-sql; Share. Improve this question. Follow edited Jan 4, 2012 at …
WebJul 14, 2024 · 1. If the Employee.Empid is the primary key, a crude way to do this could be as follows: var empDepList = (from D in db.Department join E in db.Employee on D.Empid equals E.Empid where D.Status == 1 orderby E.Empid, D.Depid descending select new { D.DepartmentName, E.EmployeeName }).FirstOrDefault (); Share.
WebJul 26, 2016 · 1 I have list as follows static List CurrentMessage = new List (); Dynamically, values assigned to this list for example: CurrentMessage.Add (new MessageDetail { UserName = 123,GroupName = somegrp, Message = somemsg }); Here, I want to take last 5 or so records. hsa practice test governmentWeb2 days ago · In my ASP.NET Core web application, I have a client sided list of Roles objects. Each of those objects contains three string properties and a string list containing the role names. I need to query entities of type Notification from a SQL Server database using EF Core and filter based on those roles. To do this, I need to check if the list ... hsa primary beneficiaryWebOct 4, 2015 · SELECT AccountNumber, OrderID,RechargeDate FROM OrderItem OI WHERE RechargeStatus = 'SUCCESS' AND AccountNumber ='" + AccountNumber + "' and DateADD (minute, -10,getdate () ) < RechargeDate AND OrderItemID <> " + OrderItemID + ""; Now, I want to rewrite the query above using LINQ. c# sql asp.net linq Share Improve … hobby airport time to arriveWebvar res = (from element in list) .OrderBy (x => x.F2) .GroupBy (x => x.F1) .Select () Now if you want to do something more complex like take the same grouping result but take the first element of F2 and the last element of F3 or something more custom you can do it by studing the code bellow hobby airport ticket agent jobsWebFeb 20, 2013 · 4 Answers. Sorted by: 54. Use descending ordering (by date, or id) and FirstOrDefault which is supported: var showPiece = context.Portraits .OrderByDescending (p => p.Date) .FirstOrDefault (); Another option, is select portrait which has max date (id) via subquery (as Evelie suggested in comments): hobby airport shuttle to car rentalWebHow to use MS-XCEP and MS-WSTEP in .NET or JavaScript to get a certificate from AD CS? Display treeviewitem as grid rows in wpf; Output logs to Xunit using Serilog Static Logger in C#; Getting "error": "unsupported_grant_type" when trying to get a JWT by calling an OWIN OAuth secured Web Api via Postman; How to select last record in a LINQ ... hobby airport shuttle serviceWebDec 7, 2010 · Solution 1 Just call Last () [ ^ ]: var lastItem = yourQuery.Last () If you think there is a chance your table may be empty, use LastOrDefault () [ ^] instead. This will return null if nothing is found. var lastItem = yourQuery.LastOrDefault (); if (lastItem == null ) { //Table was empty. } hobby airport terminal layout