如何将linq中的sql脚本转换为实体?

时间:2012-01-20 12:12:33

标签: sql entity-framework-4

SELECT * INTO #Loc1
FROM LocaleStringResource
Where LanguageId = 1

SELECT * INTO #Loc2
FROM LocaleStringResource
Where LanguageId = 3

SELECT   Loc1.Id, Loc1.LanguageId, Loc1.ResourceName, Loc1.ResourceValue,
         Loc2.Id, Loc2.LanguageId, Loc2.ResourceName, Loc2.ResourceValue
FROM         #Loc1 AS Loc1 INNER JOIN
                      #Loc2 AS Loc2 ON Loc1.ResourceName = Loc2.ResourceName

更新 我有一个名为 - LocaleStringResource的表。 列:Id,LanguageId,ResourceName,ResourceValue,

假设我的系统中有2种语言。

Id  Language
1   English
3   Bangla

Id  LanguageId        ResourceName                ResourceValue
1   1                 Admin.Address               Address
2   1                 Admin.Phone                 Phone
51  3                 Admin.Address               SpAddress
51  3                 Admin.Phone                 SpPhone

通过ResourceName,我得到了选择语言的值。所以从管理页面我想通过ResourceName查看什么是两种语言的ResourceValue。

所以我需要一个返回类似结果的结果集。

ResourceName           ResourceValue As EnglishText       ResourceValue As SpanishText
Admin.Address          Address                            SpAddress
Admin.Phone            Phone                              SpPhone

2 个答案:

答案 0 :(得分:2)

        var loc1 = Context.ObjectSet<LocaleStringResource>.Where(r => r.LanguageId == 1);
        var loc2 = Context.ObjectSet<LocaleStringResource>.Where(r => r.LanguageId == 2);
        var result = (
                        from l1 in loc1
                        join l2 in loc2 on l1.ResourceName equals l2.ResourceName 
                        select new{
                            ResourceName = l1.ResourceName,
                            EnglishText = l1.ResourceValue,
                            SpanishText = l2.ResourceValue
                        }
            ).ToList();

答案 1 :(得分:1)

实体框架不支持临时表和SELECT INTO构造,因此您无法直接转换它。您必须完成应用程序的逻辑并更改它以便它可以与EF一起使用,或者您必须将此代码包装到存储过程中并从EF调用它(顺便说一句,您必须使用列别名作为结果集,因为列名称与EF一起使用时,不得相同。