我正在尝试使用拉迪斯拉夫·马恩卡的建议来使用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Entity;
namespace SimTask.Data.EF4
{
public static class Extensions
{
public static IQueryable<T> IncludeMultiple<T
我被要求生成一份由针对SQL Server数据库的相当复杂的SQL查询驱动的报告。由于报告的站点已经使用Entity Framework4.1,我想我应该尝试使用EF和LINQ编写查询:
var q = from r in ctx.Responses
.Where(x => ctx.Responses.Where(u => u.UserId == x.UserId).Count() >= VALID_RESPONSES)
.GroupBy(x => new { x.User.AwardCit
我已经使用Entity Framework几个星期了。多年来,我一直在使用Linq-Objects和Linq-SQL。很多时候,我喜欢这样写linq语句:
from student in db.Students
from score in student.Scores
where score > 90
select student;
对于其他形式的linq,这将返回至少有一个分数大于90的不同学生。但是,在EF中,对于每个大于90的分数,此查询都会返回一个学生。
有没有人知道这种行为是否可以在单元测试中复制?这有没有可能是EF中的一个bug?
我正在使用MVC Core与EF-6.x,我使用Lambda表达式,但我的大四学生告诉我使用LINQ为什么,因为当使用lambda带where子句时,它将从数据库中提取所有数据,稍后它将应用where条件。在LINQ的情况下,如果您使用where条件,它将只提取过滤过的数据。请告诉我什么是正确的?
例如Lambda: context.tablename.where(condition);// Should I go with this
LINQ: (from T in tablename where t.(condition));// or this?
主要问题是,当我在中执行某个查询时,大约需要2秒。当通过实体框架执行时,我用分析器查看它,查询需要260秒。结果仅为72行1整数。此查询是由用户定义的筛选器生成的。
有没有人建议什么可能是错的?我在分析器中可以看到的sqlquery如下:
SELECT [Distinct1].[PersonID] AS [PersonID]
FROM (SELECT DISTINCT [Extent1].[PersonID] AS [PersonID]
FROM (SELECT [PersonViewWithExtraInfo].[PersonID] AS
当我查看EF生成的SQL查询时,我看到
SELECT [extent1].ID as ID,
[extent1].Name as Name
From(
Select myview.ID as ID,
myview.Name as Name
From myview) AS [extent1]
Where([Extent1].ID = @p_linq_0)
为什么outside select发生在inside select上?我有一个非常大的表,我可以通过外部查询轻松地从其中获得记录,但整个查询组合超时。
我的Linq查询
var result = from i in invitationEn
在我的应用程序中,我很难调整一个Entity Framework生成的查询。这是非常基本的查询,但出于某种原因,EF使用多个内部子查询,这些子查询在DB中执行得很糟糕,而不是使用联接。
这是我的LINQ代码:
Projects.Select(proj => new ProjectViewModel()
{
Name = proj.Name,
Id = proj.Id,
Total = proj.Subvalue.Where(subv =&
我尝试在数据库站点上执行一个简单的类似操作,同时使用基于泛型类型的查询构建服务。但是,我在调试时发现,使用反射执行EF.Functions.Like()并不像预期的那样工作:
The LINQ expression 'where __Functions_0.Like([c].GetType().GetProperty("FirstName").GetValue([c], null).ToString(), "%Test%")' could not be translated and will be evaluated locally.。
--使
我有两个表,如Customer和VisitDetail,我需要将下面的SQL查询转换为Linq到Sql
select *
from Customer
where id not in (select distinct CustomerId
from visitdetail
where VisitDate='2016-06-13' and SalesRepAccId=1 and
RouteId=10
) and
RouteId
我有一个查询,根据其他值,我尝试分步骤构建。主要查询是:
IQueryable<Employee> query =
from emp in context.Employees
where emp.PermanentClientId == Session.PermanentClientId
orderby emp.LastName, emp.FirstName
select emp;
然后,如果值不是null,则尝试将其添加到查询中:
if (!string.IsNullorEmpty(divId))
query = (from emp in
我需要在EF/Linq中创建以下TSQL查询,但我正在挣扎:
SELECT mi.CategoryID, cat.Category
FROM tblPropertyMaintenanceItem mi
INNER JOIN tblCategory cat
ON cat.CategoryID = mi.CategoryID
WHERE mi.PropertyID = 451
GROUP BY mi.CategoryID, cat.Category
ORDER BY cat.Category
我已经完成了一个基本的查询,但这显然给了我重复的行:
var cats = context.Mainten
我使用asp.net,c#和EF4。
我对LINQ查询感到困惑:
var queryContents = from a in context.CmsContentsAssignedToes
where a.UserId == myUserGuid
join cnt in context.CmsContents
on a.ContentId equals cnt.ContentId
where cnt.TypeContent == m
我正在尝试将此sql代码转换为linq SQL。但我也不明白医生的话...有人能帮帮我吗? select prcleunique, LibelleProjet, from projet a
where eqcleunique in (select EqCleunique from Compo where uscleunique = '{0}')
and (a.socleunique in (select socleunique from utilisat where uscleunique = '{0}') or a.socleunique is null)
我有:
Dictionary<int, List<int>> dict = new ...
var a = SomeEntity.Where(f => dict[f.key].Contains(f.someValue))
这会产生错误
LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[System.Int32] get_Item(Int32)' method
而在处理列表时,它是有效的:
List<int> l = ne
我有一个使用实体框架对数据库建模的。这个博客的问题是很难找到我想要的东西。它需要一个搜索功能,但我不确定如何用SQL和/或LINQ to Entities实现它。
现在我正在用这个LINQ查询搜索我的数据库,但它看起来应该更好。
public IEnumerable<BlogPost> SearchBlogPosts(string query, int page, int itemsPerPage)
{
var result = _dataContext.BlogPosts
.Where(BlogPostContains(qu
我在中有两个模型,相同的命名空间/模块
module ReverseAuction
class Demand < ApplicationRecord
belongs_to :purchase_order, inverse_of: :demands, counter_cache: true
end
end
module ReverseAuction
class PurchaseOrder < ApplicationRecord
has_many :demands
end
end
请注意,我不需要为模型指定class_name,因为它们在同一个模块中,而且关系
我试图在我的Linq to SQL查询中注入一个动态where子句,但我得到了一个重载异常。当添加到查询中时,相同的表达式可以正常工作吗?
qry.Where(Function(c) c.CallDate < Date.Now.AddDays(-1))
有什么关于如何工作的想法吗?
异常为:
Overload resolution failed because no accessible 'Where' can be called with these arguments:
Extension method 'Public Function Where(pred
EF Core 2.1抛出有关SQL注入的警告。这是我的代码片段
var query = await _ctx.Set<TABLE>()
.FromSql("Select * from TABLE where Artikelnummer IN (" + string.Join(',', artNum) + ")")
.AsNoTracking()
.Select(z => new
我在EF Core 2.2中将此查询转换为linq,但找不到任何正确的方法:
select r.*,v.* from Planning.RefineryUnitMonthDatas r
join Planning.RefineryUnitMonthDataValues v on r.Id = v.EntityId
join(
select EntityId, max(v.CreatedAt) CreatedAt from Planning.RefineryUnitMonthDatas r
join Planning.RefineryUnitMonthDa
我使用的是EF6,我希望在一组ID中的表中获得记录。
例如,在我的测试中,我使用了4个ID。
我尝试两种选择,第一种是任意选择。
dbContext.MyTable
.Where(x => myIDS.Any(y=> y == x.MyID));
该linq exrepsion生成的that是:
SELECT
*
FROM [dbo].[MiTabla] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM (SELECT
[UnionAll2].[