Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# (EF 4.0与普通SQL相比)提高Linq中的执行时间_C#_Sql Server_Performance_Linq - Fatal编程技术网

C# (EF 4.0与普通SQL相比)提高Linq中的执行时间

C# (EF 4.0与普通SQL相比)提高Linq中的执行时间,c#,sql-server,performance,linq,C#,Sql Server,Performance,Linq,当我执行此SQL语句时,运行它完全不需要时间: select * from [user] left join licence on [user].userID = licence.UserID left join licenceProducts on licence.licenceID = licenceProducts.licenceID left join products on products.productID = licenceProducts.productID left join

当我执行此SQL语句时,运行它完全不需要时间:

select * from [user]
left join licence on [user].userID = licence.UserID
left join licenceProducts on licence.licenceID = licenceProducts.licenceID
left join products on products.productID = licenceProducts.productID
left join contacts on contacts.UserID = [user].userID
left join usersupportedproducts on usersupportedproducts.UserID = [user].userID
left join [user] b on b.userID = [user].ParentUserID
where [user].Type <> 6
order by [user].name

我试图通过放入SQl语句来执行
ExecuteStoreQuery()
,但它没有连接
用户的关系

直接在SQl中编写查询几乎总是比在LINQ中编写表达式更有效,而后必须将其转换为“次优”SQl

您可以做的只是创建一个包含查询的
视图
,然后将该
视图
映射到实体框架中的
实体
,然后查询该
实体
——它将在SQL Server中执行
视图


另请参见。

您是否使用SQL分析器查看linq语句生成的内容?在我看来,您(在您的linq中)就像一个疯子一样渴望加载,代替左连接。如何在此Linq语句中执行左连接?@billybob这里有一个MSDN中关于如何在Linq中执行左连接的示例:使用此方法不会添加导航属性。但是我需要获得导航属性,因此这不是我想要实现的好方法。
users = null;
using (var db = new DistributorEntities())
{
    try
    {
        users = db.Users
            .Include(u => u.Licences)
            .Include(u => u.Licences.Select(l => l.LicenceProducts.Select(lp => lp.Product)))
            .Include(u => u.UserAddress)
            .Include(u => u.Contact)
            .Include(u => u.User2)
            .Include(u => u.SupportProducts)
            .Where(u => u.Type != (int)UserType.Admin)
            .OrderBy(u => u.Name)
            .ToList();
    }
    catch (Exception ex)
    {
        if (ex is InvalidOperationException)
        {
            _EventLog.WriteEntry(ex.Message + "  WebService.GetAllUsersAndChildren");
        }
        exception = new ServiceError(ex);
    }
}