Need to Convert this SQL query to LINQ - linq

can anybody help me to convert this SQL query to LINQ code in MVC? I need to return a list. The DB context entity is: _dbContext.
select distinct table1.AIG_ID, table1.GMT_NAME, table1.AIG_Number
from table1 left join table2 on table1.AIG_ID = table2.AIG_ID**

var data=(from item in db.table1 join
item1 in db.table2 on item.AIG_ID equals item1.AIG_ID
select new {item.AIG_ID ,item.GMT_NAME ,item.AIG_Number }).GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault()).ToList();
I write this part for your distinct in sql
GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault())

Related

Linq (left join) and Sql

SELECT
SI_2.[StudGUID] ,SI_2.[ClassName],
SI_2.[StudGUID],SI_1.[StuName],SI_1.[Title],SI_1.[StuMobileNumber]
FROM
[StudentInfo2] AS SI_2
INNER JOIN
[StudentInfo] AS SI_1 ON SI_2.[StudGUID]=SI_1.[StuCode]
WHERE
SI_2.[ClassID] IN (SELECT SC.[ClassID]
FROM [SchoolClass] AS SC
LEFT JOIN [BookClass] AS BC ON SC.[ClassID]=BC.[ClassID]
WHERE BC.[ClassID] IS NULL AND SchoolYear = 2015)
AND SI_2.[isMonitor] = 1
Question: I want use Linq to implement the code in SQL. How to use Linq to implement the code?
Seek help!

Need to convert SQL query to Linq contains "group by having distinct count" clause

I need to convert oracle SQL query to linq query, which contains "group by having distinct count" clause.
Below is the query:
select po.id from Purchaseorder po
inner join POlineitem poline
on poline.purchaseorderid=po.id
where po.id in(236604,240480,240972,242622,242929,243293,244535)
group by po.id having count(distinct poline.orderstatus)=1
Here Purchaseorder is the parent table which might have multiple lines in POlineitem table. poline.orderstatus can have specific status values like 1,2,3,4,5,6,7,8
Please help.
Try this query
int[] objlist={236604,240480,240972,242622,242929,243293,244535};
from a in Contex.Purchaseorder.where(x=>objlist.Contain(x.id))
join b in Contex.POlineitem.Distinct(x=>x.orderstatus) on a.id equals b.purchaseorderid
group item by a.id into groupedItems
let count = groupedItems.Count()
where count==1
select item.key

Select All columns for all tables in join + linq join

How to select all columns from tables in join using linq
Sql:
select CTRL_RUN_JOB.*, CTRL_DATA_STREAM.*
from CTRL_RUN_JOB inner join CTRL_DATA_STREAM
on CTRL_RUN_JOB.DATA_STREAM_ID= CTRL_DATA_STREAM.DATA_STREAM_ID
Linq:
from CTLJCRJOB in CTRL_RUN_JOBs
join CTLRFDSTM in CTRL_DATA_STREAMs
on CTLJCRJOB.DATA_STREAM_ID equals CTLRFDSTM.DATA_STREAM_ID
select new {
CTLJCRJOB.* // ???
,CTLRFDSTM.* // ???
}
Thanks
While you cant expand them to columns, you can simply return the entities. Eg:
select new { CTLJCRJOB, CTLRFDSTM }
If you need it flattened, then you will have to write out the mapping yourself, but will still be very trivial.
You could use the into clause, but it will not flatten it for you.
from CTLJCRJOB in CTRL_RUN_JOBs
join CTLRFDSTM in CTRL_DATA_STREAMs
on CTLJCRJOB.DATA_STREAM_ID equals CTLRFDSTM.DATA_STREAM_ID into ALLCOLUMNS
from entry in ALLCOLUMNS
select entry
Another twist is
OutPutList = (from CTLJCRJOB in CTRL_RUN_JOBs
join CTLRFDSTM in CTRL_DATA_STREAMs
on CTLJCRJOB.DATA_STREAM_ID equals CTLRFDSTM.DATA_STREAM_ID
select CTLJCRJOB).ToList();
You could use the into clause, but it will not flatten it for you.
from CTLJCRJOB in CTRL_RUN_JOBs
join CTLRFDSTM in CTRL_DATA_STREAMs
on CTLJCRJOB.DATA_STREAM_ID equals
CTLRFDSTM.DATA_STREAM_ID into ALLCOLUMNS
from entry in ALLCOLUMNS
select entry
in this way we can only get CTLJCRJOB columns result, there was no CTLRFDSTM table columns through my test

collation conflict

Is there any one who knows how we can solve collation issue in select linq query?
I'm getting this error when I want to select data in linq.
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation
var lstData = from s in dataTrackDB.datas
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join m in dataTrackDB.mktDatas on s.mktcode equals m.mktcode
select new dataView {
Account=m.account,
brandcode=b.brandcode,
commodity=s.commodity,
date=s.date,
daysvalid=s.daysvalid,
mfrcode=b.mfrcode,
mktcode=s.mktcode,
price=s.price,
prodid=s.prodid,
statecode=s.statecode,
subcommodity=s.subcommodity,
supprecode=s.supprecode,
units =s.units
};
lstData = lstData.AsQueryable().Where(x => x.mfrcode == mfr );
return lstData.Take(100).ToList();
The problem is not in Linq but in your database
you can for example create a view that joins that way and the select the data in linq from the view
SELECT * FROM T1
INNER JOIN T2 ON
T1.Name COLLATE Latin1_General_CI_AS = T2.Name COLLATE Latin1_General_CI_AS
or select the data first in linq2sql separately for each table and then join it with linq2object
add COLLATE DATABASE_DEFAULT at the end of the query

Linq To Entity Framework selecting whole tables

I have the following Linq statement:
(from order in Orders.AsEnumerable()
join component in Components.AsEnumerable()
on order.ORDER_ID equals component.ORDER_ID
join detail in Detailss.AsEnumerable()
on component.RESULT_ID equals detail.RESULT_ID
where orderRestrict.ORDER_MNEMONIC == "MyOrderText"
select new
{
Mnemonic = detail.TEST_MNEMONIC,
OrderID = component.ORDER_ID,
SeqNumber = component.SEQ_NUM
}).ToList()
I expect this to put out the following query:
select *
from Orders ord (NoLock)
join Component comp (NoLock)
on ord .ORDER_ID = comp.ORDER_ID
join Details detail (NoLock)
on comp.RESULT_TEST_NUM = detail .RESULT_TEST_NUM
where res.ORDER_MNEMONIC = 'MyOrderText'
but instead I get 3 seperate queries that select all rows from the tables. I am guessing that Linq is then filtering the values because I do get the correct values in the end.
The problem is that it takes WAY WAY too long because it is pulling down all the rows from all three tables.
Any ideas how I can fix that?
Remove the .AsEnumerable()s from the query as these are preventing the entire query being evaluated on the server.

Resources