Nested Select queries I main select query LINQ

时间:2015-06-15 14:52:26

标签: c# linq

I hardly use LINQ rather I use MSDAAB as DAL which I use to execute my inline sql query or my db store procedure etc. Now I am in situation where I need to use LINQ to extract data from datatable.

Here I am attaching one screenshot like how my data look like:

enter image description here

The data is coming from sql server and for which I am using the below query for sql server. Please see my query first.

select 
(select count(*) as incoming from tridip_Interval where direction='I' 
and CONVERT(datetime,right([Call Start],8)) >='08:30:00' 
and CONVERT(datetime,right([Call Start],8)) <='09:00:00' 
and Is_Internal=0 and continuation=0 and 
RIGHT(convert(varchar,[call duration]),8)<> '00:00:00' 
and party1name='Train5') as incoming, 

(select count(*) as OutGoing from tridip_Interval where direction='O' 
and CONVERT(datetime,right([Call Start],8)) >='08:30:00' 
and CONVERT(datetime,right([Call Start],8)) <='09:00:00' 
and Is_Internal=0 and continuation=0  
and party1name not in ('Voice Mail') 
and party1name='Train5') as OutGoing, 

(select count(*) as CallTransfer from tridip_Interval where continuation=1  
and CONVERT(datetime,right([Call Start],8)) >='08:30:00' 
and CONVERT(datetime,right([Call Start],8)) <='09:00:00' 
and RIGHT(convert(varchar,[call duration]),8)<> '00:00:00' and 
party1name not in ('Voice Mail') 
and party1name='Train5') as CallTransfer, 

(SELECT count(*) as UnansweredCalls_DuringBusinessHours 
from tridip_Interval where direction='I' and 
CONVERT(datetime,right([Call Start],8)) >='08:30:00' 
and CONVERT(datetime,right([Call Start],8)) <='09:00:00' 
and RIGHT(convert(varchar,[call duration]),8)= '00:00:00' 
and [Ring duration]>0 and party1name='Train5') as misscall

Now I want to use LINQ to query data table but hence I am not good in linq so not being able to compose the above sql like query with linq. So just wonder if anyone can help me to compose the above query with linq.

I apologize if could not highlight my linq query because very honestly I know linq very little and that is the reason things is not coming to my mind how to compose the same above sql query with LINQ. Looking for hep and support.

1 个答案:

答案 0 :(得分:2)

Linq,这个过程,只是针对一个独特的数据列表

第一步是使用ORM(对象关系映射器),例如实体框架(EF)。通过EF将表和存储过程的世俗映射到实体中,可以专注于显示数据或处理数据。

因此现在就是Linq的用武之地,因为EF会使每个表都成为数据列表

我建议学习Linq最好的工具之一是有一个名为Linqpad的开发人员专业编辑器。使用Linqpad(具有对象智能感知的高级功能),它是制作linq查询的最佳工具。

  

如何学习Linq?

首先从您正在执行的表中取出10个实体并运行该工具。看看数据。然后添加.Where( )子句并再次查看显示的数据。请注意,Linqpad .Dump()上有一个特定于工具的扩展,可以显示数据。

了解Select,这是投影扩展程序。投影意味着将当前的数据列表投射到新的内容中。可以是新的匿名实体,也可以是想要返回的特定类的实例的新列表。

继续添加查询,直到您创建了要实现的动态实体。

  

但我主要在SQL工作......

仍然在SQL中思考?

Linqpad将向您展示由Linq执行的生成的SQL,以实现其结果。还可以对数据库运行SQL查询! 一个带有原始SQL查询的窗口和一个窗口Linq如何帮助人们学习。

这一好处可能会为开发Linq查询提供一个见解。

这是我自学的方式在Linq中思考,我从未回头。

我最近在我的文章Entity Framework Stored Procedure Instructions中揭示了如何使用存储过程和EF来实现linq的最终目标。