我正在使用Telerik Open Access。我有两个单独的项目,其中包含Open Access数据,然后是第三个包含大量代码的项目。我一直在努力将一个简单的(至少我认为是)SQL查询转换为LINQ,以便我可以获得所需的数据。我没有成功。由于需要Trim()函数(我认为),我不得不将单个LINQ查询分解为单独的查询。这导致了一段冗长的代码,我仍然没有得到与我的SQL查询相同的结果。
所以我的问题是,无论如何使用SQL而不是LINQ来访问Open Access项目中的数据?如果是这样,你能告诉我为我的查询做这个的语法吗?
如果无法使用SQL,您能告诉我向我展示将SQL查询转换为LINQ的正确方法,以便得到相同的结果吗?
谢谢。
我的SQL查询是
SELECT DISTINCT us2.ccustno, us2.dispname, us2.csiteno, so.s1_name
FROM [DALubeDeacom].[dbo].[dmbill] bi
INNER JOIN [DALubeDeacom].[dbo].[dmso1] so
ON bi.bi_s1id = so.s1_id
INNER JOIN [DALubeNew].[dbo].[usersecurity] us2
ON so.s1_name = us2.cparentno
WHERE
us2.ctype = 'JOBSITE'
AND us2.csiteno is not null
AND us2.csiteno != ''
AND bi.bi_smid = '22'
ORDER BY us2.csiteno
我的LINQ查询是
public List<DataModelSample> GetLocationsBySalesNo(string salesNo)
{
int iSalesNo = int.Parse(salesNo.Trim());
try
{
var dmso = (
from so in deacom.Dmso1
join qt in deacom.Dmbills
on so.S1_id equals qt.Bi_s1id
where qt.Bi_smid == iSalesNo
select new Dmso1
{
S1_id = so.S1_id
, S1_name = so.S1_name.Trim()
}
);
var usec = (
from us in dbContext.Usersecurities
where us.Cparentno != null && us.Cparentno.Trim() != "" && us.Ctype.Trim() == "JOBSITE" && us.Csiteno.Trim() != ""
select new Usersecurity
{
Ccustno = us.Ccustno.Trim(),
Csiteno = us.Csiteno.Trim(),
Dispname = us.Dispname.Trim(),
Cparentno = us.Cparentno.Trim()
}
);
var customers =
(
from us in usec
join so in dmso
on us.Cparentno equals so.S1_name
select us
);
customers = customers.GroupBy(x => x.Csiteno).Select(x => x.First());
List<DataModelSample> listLocations =
(
from c in customers
select new DataModelSample
{
customerID = c.Ccustno
,
origLocationName = c.Csiteno + " " + c.Dispname
,
origLocationID = c.Csiteno
}
).OrderBy(x => x.origLocationID).ToList();
return listLocations.ToList();
}
catch (Exception ex)
{
throw ex;
}
} // GetLocationsBySalesNo(userInfo.csalesno)
编辑1 - 2-19-16
尝试了ViktorZ的建议。他的询问与我第一次尝试的相似。它返回了错误&#34; Identifier&#39; Ctype&#39;不是DALube_DeacomModel.Dillill&#39; DALube_DeacomModel.Dmbill&#39;的参数或变量或字段。如果&#39; Ctype&#39;是属性请向其添加FieldAlias或Storage属性或将其声明为字段的别名。&#34;从在线搜索来看,这似乎是对#34;扩展字段&#34;。我似乎没有使用这样的领域。我可以解决这个错误的唯一方法是在原始问题中将其分解为较小的LINQ查询,这些查询没有产生正确的结果。有什么建议吗?
以下是代码:
var query = (from bill in deacom.Dmbills
join so in deacom.Dmso1 on bill.Bi_s1id equals so.S1_id
join us in dbContext.Usersecurities on so.S1_name equals us.Cparentno
where us.Ctype == "JOBSITE"
&& us.Csiteno != null
&& us.Csiteno != string.Empty
&& bill.Bi_smid == iSalesNo
select new
{
ccustno = us.Ccustno.Trim(),
dispname = us.Dispname.Trim(),
csiteno = us.Csiteno.Trim(),
s1_name = so.S1_name.Trim()
}).Distinct();
答案 0 :(得分:0)
SQL查询的一个非常粗略的近似值是:
var query = (from bill in deacom.Bills
join so in deacom.LubeDeacom on bill.bi_s1id equals so.s1_id
join us in deacom.UserSecurity on so.s1_name equals us.cparentno
where us.ctype = "JOBSITE"
&& us.csiteno != null
&& us.csiteno != string.Empty
&& bill.smid = '22'
order by us.csiteno
select new
{
us.ccustno.Trim(),
us.dispname.Trim(),
us.csiteno.Trim(),
so.s1_name.Trim()
}).Distinct();
// to check the translation result
string sql = query.ToString()
// to get the results
var result = query.ToList()
如果这对您不起作用,您可以随时回退到Telerik Data Access ADO.NET API。这是documentation article如何使用它。