Tag: linq to entities

在linq-to-entities的一对多连接中只获得一条(最后一条)记录

我在linq-to-entities中有以下内容 clientprojects = (from p in this.SAPMappingEntities.SAP_Master_Projects join c in this.SAPMappingEntities.SAP_Master_ProjectPartners on c.project_no equals p.project_no where c.partner_name.Contains(clientstring) orderby p.start descending select new ClientProjects { client = c.partner_name, location = c.city +”, “+c.region, project_no = c.project_no, start_dt = p.start, end_dt = p.finish }).Take(50).ToList(); 我想更改此查询,以便每个SAP_Master_Project只获取具有最新update_dt的SAP_Master_ProjectPartners记录。 我怎样才能做到这一点? 编辑 有一个项目表,其中包含项目编号和项目详细信息,包括项目开始和结束日期。 有一个项目合作伙伴表,其中包含项目合作伙伴编号,名称,项目编号,更新日期和其他详细信息。 SAP_MASTER_PROJECT project_no 开始 完 SAP_MASTER_PROJECTPARTNERS partner_no project_no PARTNER_NAME […]

与Linq的明确日期

我有一个名为Billings的实体,其属性为DateTime。 我需要独特地找到日期,以便我可以浏览它们并做一些事情。 我试过了: var DistinctYearMonthsDays = (from t in db.Billings where t.DateTime.HasValue select t.DateTime.Value.Date).Distinct(); foreach (var day in DistinctYearMonthsDays) 但我得到了(在foreach ): LINQ to Entities不支持指定的类型成员“Date”。 仅支持初始化程序,实体成员和实体导航属性。 搜索后我也尝试了以下但没有成功: IEnumerable DistinctYearMonthsDays = db.Billings .Select(p => new { p.DateTime.Value.Date.Year, p.DateTime.Value.Date.Month, p.DateTime.Value.Date.Day }).Distinct() .ToList() .Select(x => new DateTime(x.Year,x.Month,x.Day)); 任何帮助将非常感谢。

如何在Entity Framework中的同一查询中使用Include和Anonymous Type?

在如何计算使用Where In Entity Framework中的关联实体我得到此查询 但是当我访问queryResult [0] .post.Category或queryResult [0] .post.Tags时它总是空的,因为我没有使用Include。 包括不适用于Projection,正如微软在最后一项所述: http : //msdn.microsoft.com/en-us/library/bb896317.aspx var queryResult = (from post in posts join comment in comments.Where(x=> x.IsPublic) on post.Id equals comment.Post.Id into g select new { post, post.Author, post.Tags, post.Categories, Count = g.Count() }) 如何在同一个查询中获取Count,并包含与标签和类别的关系? 为什么EF关系修复不起作用?

当相关对象在.Include中为null时,Entityframework Core 2.0错误

我正在使用EF core 2.0和.Net core2.0来开发我的webapi应用程序。 在下面的查询中,当案例没有工作流程(即可空的工作流程)时,我会遇到exception。 我得到的错误是’System.NullReferenceException:对象引用未设置为对象的实例。’ 当c.workflow为null时,我怀疑.ThenInclude()上的exception。 案例实体: public class Case { [Key] public int CaseId { get; set; } public int? WorkflowId { get; set; } public Workflow Workflow { get; set; } } 查询: var item = await _context.Cases .Include(c => c.CaseDetail) .Include(c => c.Workflow).ThenInclude(wf => wf.CurrentWorkflowLevel) .Include(c => c.Alerts) .FirstOrDefaultAsync(c => c.CaseId […]

如何在EF中执行日期部分比较

我听说人们说日期时间比较不起作用只是因为时间部分,因为datetime有时间部分。 在SQL中我总是像这样比较日期时间,它工作正常 select * from employee where convert(varchar,dob,112) > ‘20111201’ // this yyyymmdd format. 我怎么能在LINQ查询中模拟这个?

使用Entity Framework从SQL数据库中获取所有内容

我有这样的产品清单 var r = db.Products.Where(x => x.Sites .Where(z => z.Key == associatedProducts.Key) .Any() ).ToList() 有一个名为Products的实体,我想从产品中获取所有元素,但那些存在于associatedProducts.Products中 我怎样才能做到这一点 ?

如何在必要时使用ADO.NET Entity Framework加载varbinary(max)字段?

我的一个表中有一个varbinary(max)字段,但我不是每次都需要它,而且我正在寻找一种只在必要时从数据库中获取它的方法。 我正在使用ADO.NETentity framework。 怎么做?

不支持Linq-to-EF DateTime.ToLocalTime

Linq到EF不支持DateTime.ToLocalTime 。 有什么选择? 我的想法已经不合时宜了。

动态添加新的lambda表达式以创建filter

我需要对ObjectSet进行一些过滤,以通过这样做获取我需要的实体: query = this.ObjectSet.Where(x => x.TypeId == 3); // this is just an example; 稍后在代码中(以及在启动延迟执行之前)我再次过滤查询,如下所示: query = query.Where(); 到目前为止,这非常有效。 这是我的问题: 实体包含DateFrom属性和DateTo属性,它们都是DataTime类型。 它们代表了一段时间 。 我需要过滤实体,只获得那些属于时间段 集合的实体。 集合中的句点不一定是连续的 ,因此,检索实体的逻辑看起来像: entities.Where(x => x.DateFrom >= Period1.DateFrom and x.DateTo x.DateFrom >= Period2.DateFrom and x.DateTo <= Period2.DateTo) || ……以及集合中所有时期的不断变化。 我试过这样做: foreach (var ratePeriod in ratePeriods) { var period = ratePeriod; query […]

GroupBy with Count

我有列表testList返回,它有5个项目与适当的值。 var testList = _context.LectureReportStudent .Include(x => x.LectureReport) .Include(x => x.LectureReport.Lecture) .Include(x => x.Student) .Where(x => stuIds.Contains(x.LectureReport.LectureId)) .Select(x => new StudentModel { Name = x.Student.FirstName + “” + x.Student.LastName, Position = x.Student.JobTitle, JobId = x.LectureReport.LectureId, StudentId = x.StudentlId, LectureId = x.LectureReportId, Date = x.Date }; 我在StudentId上进一步GroupBy这个列表。 var result = testList.GroupBy(x => x.StudentId) 每个项目都有以下型号 Date, DaysOnLecture, […]