0

I'm trying to write a LINQ for the below query,

Select DIS.iDataItemID, DIS.iDataItemCurrentStatusID, DI.iDataTypeID, 
DI.iEmployeeID, DI.iEmployerID, DI.iLinkID, DI.iSystemID, DI.sData, 
DI.sEmployerCode, DI.sRecieptID, COunt(*)
from OT_BackendUpdate_DataItem DI
INNER JOIN OT_BackendUpdate_DataItemStatus DIS ON DIS.iDataItemID = 
DI.iDataItemID 
INNER JOIN OT_BackendUpdate_RefDataItemStatus ON DIS.iDataItemCurrentStatusID 
= OT_BackendUpdate_RefDataItemStatus.iDataItemStatusID
AND DIS.iDataItemCurrentStatusID = 1 AND  
DIS.iDataItemCurrentStatusID IN (
SELECT top 1 iDataItemCurrentStatusID FROM OT_BackendUpdate_DataItemStatus 
Where 
OT_BackendUpdate_DataItemStatus.iDataItemID = DI.iDataItemID
order by OT_BackendUpdate_DataItemStatus.dDateEffective desc
)
GROUP BY DIS.iDataItemID, DIS.iDataItemCurrentStatusID, DI.iDataTypeID, 
DI.iEmployeeID, DI.iEmployerID, DI.iLinkID, DI.iSystemID, DI.sData, 
DI.sEmployerCode, DI.sRecieptID

I have written the LINQ as below, Need to know how to use Group by on this,

_result = from _dataItem in _msaDBContext.OT_BackendUpdate_DataItem
                                 join _dataItemStatus in _msaDBContext.OT_BackendUpdate_DataItemStatus on _dataItem.iDataItemID equals _dataItemStatus.iDataItemID
                                 join _refDataItemStatus in _msaDBContext.OT_BackendUpdate_RefDataItemStatus on _dataItemStatus.iDataItemCurrentStatusID equals _refDataItemStatus.iDataItemStatusID
                                 where _dataItemStatus.iDataItemCurrentStatusID == _processingStatus.iDataItemStatusID && _systemIDs.Contains(_dataItem.iSystemID) && _dataItem.iDataTypeID == _dataType.iDataTypeID
                                 && (_msaDBContext.OT_BackendUpdate_DataItemStatus.Where(x=>x.iDataItemID == _dataItem.iDataItemID).OrderByDescending(x=>x.dDateEffective).Select(x=>x.iDataItemCurrentStatusID).Take(1)).Equals(_dataItemStatus.iDataItemCurrentStatusID)
                                 group _refDataItemStatus by new
                                 {
                                     _dataItemStatus.iDataItemCurrentStatusID,
                                     _dataItemStatus.iDataItemID,
                                     _dataItem.iDataTypeID,
                                     _dataItem.iEmployeeID,
                                     _dataItem.iEmployerID,
                                     _dataItem.iLinkID,
                                     _dataItem.iSystemID,
                                     _dataItem.sData,
                                     _dataItem.sEmployerCode,
                                     _dataItem.sRecieptID
                                 } into g
                                 select new
                                 {
                                     iDataItemID = g.Key.iDataItemID,
                                     iDataItemCurrentStatusID = g.Key.iDataItemCurrentStatusID,
                                     iDataTypeID = g.Key.iDataTypeID,
                                     iLinkID = g.Key.iLinkID,
                                     sData = g.Key.sData,
                                     iEmployeeID = g.Key.iEmployeeID,
                                     iEmployerID = g.Key.iEmployerID,
                                     sEmployerCode = g.Key.sEmployerCode,
                                     iSystemID = g.Key.iSystemID,
                                     sReceiptID = g.Key.sRecieptID,
                                     Count = g.Count()
                                 };

The LINQ query is failing in the line,

(_msaDBContext.OT_BackendUpdate_DataItemStatus.Where(x=>x.iDataItemID == _dataItem.iDataItemID).OrderByDescending(x=>x.dDateEffective).Select(x=>x.iDataItemCurrentStatusID).Take(1)).Equals(_dataItemStatus.iDataItemCurrentStatusID)

Please let me know, how do i write the SQL IN sub-query condition using LINQ.

PFB the exception details from the above LINQ line where i'm trying to write a sub query,

"Unable to cast the type 'System.Byte' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types."

Please let me know what am i missing here.

Aryan M
  • 571
  • 3
  • 12
  • 33
  • Try this instead: `_msaDBContext.OT_BackendUpdate_DataItemStatus.Where(x=>x.iDataItemID == _dataItem.iDataItemID).OrderByDescending(x=>x.dDateEffective).Select(x=>x.iDataItemCurrentStatusID).FirstOrDefault() == _dataItemStatus.iDataItemCurrentStatusID` – tukaef Jul 31 '18 at 04:14
  • Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) would help you. – NetMage Jul 31 '18 at 21:41

0 Answers0