select ROW_NUMBER() over (order by m.Id) RowNumber,
ROW_NUMBER() over (partition by m.Rack order by m.Id) Rankvendor,
m.Id,
m.Rack,
m.Capacity,
s.BatchId,
s.SampleQty
from [MstStorage] M LEFT JOIN [tblStorageBatch] S ON M.Id=S.StorageId
---------------My Linq Code------------------------
List
try
{
_TestRetensionList = (from MstStorage in _dbContext.MstStorage.Where(k => k.IsActive == ApplicationData.IsActive)
join TblStorageBatch in _dbContext.TblStorageBatch on MstStorage.Id equals TblStorageBatch.StorageId into TblStorageBatch
from m1 in TblStorageBatch.DefaultIfEmpty()
select new
{
Id = MstStorage.Id,
Row = MstStorage.Row,
Shelf = MstStorage.Shelf,
Tray = MstStorage.Tray,
Capacity = MstStorage.Capacity,
LabId = MstStorage.LabId,
BatchId = m1.BatchId,
SampleQty = m1.SampleQty,
Rack = MstStorage.Rack
}).AsEnumerable().Select((k, index) => new TestRetensionList()
{
Id = k.Id,
Row = k.Row,
Shelf = k.Shelf,
Tray = k.Tray,
Capacity = k.Capacity,
LabId = k.LabId,
BatchId = k.BatchId,
SampleQty = k.SampleQty,
Rack = k.Rack,
LeftQty = (k.Capacity - k.SampleQty),
Rank= ??
Statuscolor = Statuscolor((int)(k.Capacity - k.SampleQty), k.SampleQty).Result,
btnvisible= BtnVisibility((int)(k.Capacity - k.SampleQty), k.SampleQty).Result,
}).ToList();

Nabeel Hassan4 reputationPosted Jul 14, 2023, 9:31 AM EDT
var query = (from m in _dbContext.MstStorage.Where(k => k.IsActive == ApplicationData.IsActive) join s in _dbContext.TblStorageBatch on m.Id equals s.StorageId into storageBatches from sb in storageBatches.DefaultIfEmpty() orderby m.Id select new { RowNumber = 0, // Will be set later Rankvendor = 0, // Will be set later m.Id, m.Rack, m.Capacity, BatchId = sb.BatchId, SampleQty = sb.SampleQty }).ToList(); var result = query.Select((m, index) => new TestRetensionList { Id = m.Id, Rack = m.Rack, Capacity = m.Capacity, BatchId = m.BatchId, SampleQty = m.SampleQty, RowNumber = index + 1, Rankvendor = query.Where(x => x.Rack == m.Rack && x.Id <= m.Id).Count(), // Other properties }).ToList();Sandeep KumarRank #1,642 · 937 reputationPosted Jul 15, 2023, 1:28 AM EDT
Your Query Result not matched against my sql query,
pls look into it
Mariraj K90 reputationPosted Jul 24, 2023, 3:19 AM EDT
using (var context = new YourDbContext()) { var query = from m in context.MstStorage join s in context.tblStorageBatch on m.Id equals s.StorageId into storageBatchJoin from s in storageBatchJoin.DefaultIfEmpty() select new { RowNumber = (from m2 in context.MstStorage where m2.Id <= m.Id select m2).Count(), Rankvendor = (from m2 in context.MstStorage where m2.Rack == m.Rack && m2.Id <= m.Id select m2).Count(), m.Id, m.Rack, m.Capacity, s.BatchId, s.SampleQty }; // Execute the query var result = query.ToList(); }
Edwin Klesman17 reputationPosted Sep 7, 2023, 8:52 AM EDT
//LINQ query var query = from m in MstStorage join s in tblStorageBatch on m.Id equals s.StorageId into m_s from s in m_s.DefaultIfEmpty() orderby m.Id select new { RowNumber = m.Id, Rankvendor = m.Rack, m.Id, m.Rack, m.Capacity, s.BatchId, s.SampleQty }; //Executing the LINQ query var result = query.ToList();PS: I generated this with my (sideproject gone product) linqmeup.com
Prasad RaveendranRank #320 · 7.8K reputationPosted Sep 10, 2023, 12:22 PM EDT
var query = (from m in dbContext.MstStorage join s in dbContext.tblStorageBatch on m.Id equals s.StorageId into storageBatches from sb in storageBatches.DefaultIfEmpty() orderby m.Id select new { m.Id, m.Rack, m.Capacity, BatchId = sb == null ? null : (int?)sb.BatchId, SampleQty = sb == null ? null : (int?)sb.SampleQty }) .AsEnumerable() // Switch to LINQ to Objects for row numbering .Select((x, index) => new { RowNumber = index + 1, RankVendor = x.Rack != null ? x.Rack + "-" + (index + 1) : (index + 1).ToString(), x.Id, x.Rack, x.Capacity, x.BatchId, x.SampleQty });We first perform the join and ordering as before.
We use
.AsEnumerable()to switch to LINQ to Objects, allowing us to use theEnumerable.Selectmethod for generatingRowNumberandRankVendor.In the final
Selectmethod, we use theindexparameter to calculateRowNumber. We also computeRankVendorbased on theRackcolumn, appending the row number ifRackis not null, or just using the row number itself ifRackis null.This may help you
Prasad RaveendranRank #320 · 7.8K reputationPosted Sep 10, 2023, 12:26 PM EDT
I have provided the updated query
var query = (from m in dbContext.MstStorage join s in dbContext.tblStorageBatch on m.Id equals s.StorageId into storageBatches from sb in storageBatches.DefaultIfEmpty() orderby m.Id select new { m.Id, m.Rack, m.Capacity, BatchId = sb == null ? null : (int?)sb.BatchId, SampleQty = sb == null ? null : (int?)sb.SampleQty }) .AsEnumerable() // Switch to LINQ to Objects for row numbering .Select((x, index) => new { RowNumber = index + 1, RankVendor = x.Rack != null ? (index + 1) : 0, // Reset RankVendor for each new Rack value x.Id, x.Rack, x.Capacity, x.BatchId, x.SampleQty });In this corrected query:
We first perform the join and ordering as before.
We use
.AsEnumerable()to switch to LINQ to Objects, allowing us to use theEnumerable.Selectmethod for generatingRowNumberandRankVendor.In the final
Selectmethod, we use theindexparameter to calculateRowNumber. We also computeRankVendorby resetting it to 0 whenever a newRackvalue is encountered.This should correctly calculate the
RowNumberbased onm.IdandRankVendorbased onm.Rack.Amit MohantyRank #112 · 20K reputationPosted Sep 11, 2023, 2:21 AM EDT
var _TestRetensionList = _dbContext.MstStorage .Where(k => k.IsActive == ApplicationData.IsActive) .GroupJoin( _dbContext.TblStorageBatch, mstStorage => mstStorage.Id, tblStorageBatch => tblStorageBatch.StorageId, (mstStorage, tblStorageBatch) => new { MstStorage = mstStorage, TblStorageBatch = tblStorageBatch } ) .SelectMany( x => x.TblStorageBatch.DefaultIfEmpty(), (x, m1) => new { Id = x.MstStorage.Id, Row = x.MstStorage.Row, Shelf = x.MstStorage.Shelf, Tray = x.MstStorage.Tray, Capacity = x.MstStorage.Capacity, LabId = x.MstStorage.LabId, BatchId = m1?.BatchId, SampleQty = m1?.SampleQty, Rack = x.MstStorage.Rack } ) .OrderBy(k => k.Id) .Select((k, index) => new TestRetensionList() { Id = k.Id, Row = k.Row, Shelf = k.Shelf, Tray = k.Tray, Capacity = k.Capacity, LabId = k.LabId, BatchId = k.BatchId, SampleQty = k.SampleQty, Rack = k.Rack, LeftQty = (k.Capacity - k.SampleQty), Rank = index + 1, // Assign the rank using the index + 1 Statuscolor = Statuscolor((int)(k.Capacity - k.SampleQty), k.SampleQty).Result, btnvisible = BtnVisibility((int)(k.Capacity - k.SampleQty), k.SampleQty).Result, }) .ToList();