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
Related
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())
I have a Linq queries that have tables join and couple of tables inner join together. Sometimes I got an error from the query when table is empty. What I trying to do is I am tryting to get a value from table even if other table is empty.
Thanks in Advance.
You need to do left join
Assuming left join between customer and order table.
var query =
from customer in dc.Customers
from order
in dc.Orders
.Where(o => customer.CustomerId == o.CustomerId)
.DefaultIfEmpty()
select new { Customer = customer, Order = order }
Also refer below link
http://forums.asp.net/t/1792428.aspx/1
I have two db.Table1 and db.Table2 with columns named IdUser,Value
I think I should have some join but i miss the logic
it's just a logic it's not a code
how can do something like :
var total = Sum(db.Table1(Sum(Value))-db.Table2(Sum(Value))
.Where(db.Table1.IdUser=db.Table2.IdUser)
Join the tables and group
var total = from table1record in Table1
join table2record in Table2 on table1Record.IdUser equals table2Record.IdUser
group new { table1record,table2record } by table1record.IdUser into groupedRecords
select groupedRecords.Sum(x=>x.Table1Value) - groupedRecords.Sum(x=>x.Table2Value);
i need select some fields from a table in the query using join, but in the select new statement i dont have acess from the fields of the join table.
var query = from p in persistencia.RequisicaoCompraItems
join s in persistencia.Suprimentos on p.SuprimentoID equals s.SuprimentoID
(i need get fields from this join)
group p by new {p.SuprimentoID, p.RequisicaoCompraItemID, p.RequisicaoCompraID } into x
from res in x
orderby x.Key.SuprimentoID
select new {res.SuprimentoID ,
res.RequisicaoCompraItemID,
**but in here i cant acess** };
Cheers**
They're in res.Key.SuprimentoID etc
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.