Tag: linq to entities

将Linq表达式作为字符串传递?

以下代码工作正常 using (var ctx = new MyEntities()) { var devices = ctx.Devices .Where(x=> x.Device == “TEST”) .ToList(); return devices; } 我想要做的是传入“Where”子句中的表达式。 我看到它可能需要一个字符串,但以下引发错误: String expression = “x=> x.Device == \”TEST\”” ; using (var ctx = new MyEntities()) { var devices = ctx.Devices .Where(expression) .ToList(); return devices; } 运行时的错误消息是“查询语法无效。 近期’>’,第6行,第4栏。“; 传递最初从字符串派生的表达式的最佳方法是什么?

Linq-to-Entities离开JOIN

这是我的查询: from forum in Forums join post in Posts on forum equals post.Forum into postGroup from p in postGroup where p.ParentPostID==0 select new { forum.Title, forum.ForumID, LastPostTitle = p.Title, LastPostAddedDate = p.AddedDate }).OrderBy(o=>o.ForumID) 目前,Join不是左连接,这意味着如果某个论坛没有属于它的post,则不会返回。 没有post的论坛必须返回post属性的null(或默认)值。 UPDATE 结果集应该是这样的: ForumId | ForumTitle | LastPostTitle | LastPostAddedDate ——–+————+—————+—————— 4 | Sport | blabla | 12/4/2010 4 | Sport […]

LINQ to Entities Union正在抛出错误

我成功完成了以下工作: var transactions = from t in context.Transactions group t.Create_Date_Time by t.Participation_Id into t1 select new { ParticipationId = t1.Key, CreateDateTime = t1.Max() }; var cases = from c in context.Cases group c.Create_Date_Time by c.Participation_Id into c1 select new { ParticipationId = c1.Key, CreateDateTime = c1.Max() }; var interactions = from i in context.Interactions join […]

运算符’==’不能应用于linq到实体的’System.Guid’和’string’类型的操作数

我收到此错误’运算符’==’不能应用于linq中类型’System.Guid’和’string”的操作数到代码下面的entityframework。 在下面的代码中,CustomerId是Guid,customerProfileId是字符串。 var accountQuery = from C in CustomerModel.CustomerProfile where C.CustomerId == customerProfileId // Error here select C;

如何在entity framework中查找具有指定日期范围列表的日期?

我有一个IEnumerable我创建的类包含日期范围。 那个class看起来像这样: public class Range where T: struct { public T Start { get; set; } public T End { get; set; } } 我想查找我的集合中的所有记录,其中日期列落在指定日期范围的任何一个范围内。 这是我的尝试: deals = deals.Where( deal => criteria.DateRanges.Any( dt => deal.CloseDate >= dt.Start && deal.CloseDate < dt.End.Value.AddDays(1))); 这会抛出我假设的错误,因为EF不知道如何将criteria.DateRanges.Any()转换为SQL。 那你怎么写这个来查找与任何日期范围相匹配的日期?

将SQL查询转换为Linq(包含左连接)

我有一个在SQL中完美运行的查询,但我有最大的时间将其转换为linq。 该表(下面的表1)保存了多种记录类型的状态更改。 连接需要设置两个字段来创建有效连接:SubmissionId(状态所属的表的pk)和SubmissionTypeId(确定状态所属的表)。 CREATE TABLE ##Table1 (Id int, Status varchar(50), SubmissionId int, SubmissionTypeId int) insert into ##Table1(Id, Status, SubmissionId, SubmissionTypeId) select 1 ,’Status1′ ,1 , 1 union select 2,’Status2′,1, 2 CREATE TABLE ##Table2 (ID int, Value varchar(50)) insert into ##Table2 (ID, Value) select 1, ‘Value1Table2’ CREATE TABLE ##Table3 (ID int, Value varchar(50)) insert into ##Table3 […]

如何从表记录中选择除某些列以外的表记录

我有一个从sql数据库创建的实体数据库。 我需要在datagridview上显示记录,我正在使用此代码。 DBEntities db = new DBEntities(); dataGridView1.DataSource = db.Agent.Select(x => new { Name = x.Name, Second_Name = x.Second_Name}).ToList(); 例如,真正的代理表包含大约10列,我需要显示所有,exept’id’。 如果我为每8列做同样的事情,那么就变成一条长而无意义的行。 怎么做更多的遗忘和好。

嵌套LINQ返回此方法无法转换为存储表达式exception

以下LINQ: retval = ( from jm in entities.JobMasters where jm.UserId == userId && jm.IsRemote == false select new JobDto { JobMasterId = jm.JobMasterId, ExternalTaskId = jm.ExternalTaskId, JobDetails = ( from jd in entities.JobDetails where jd.JobMasterId == jm.JobMasterId select new JobDetailDto { ScreenFieldId = jd.ScreenFieldId, FieldValue = jd.FieldValue } ).ToList() } ).ToList(); 给我这个错误: LINQ to Entities无法识别方法’System.Collections.Generic.List`1 […]

entity framework:无法创建类型为’System.Collections.Generic.IList`1’的常量值

这使我今天无法解决问题。 我有这个简单的查询 var result = DataContext.Accommodations.Where(a => (criteria.MinPrice == null || a.AccommodationRates.Any(r => r.From >= criteria.MinPrice)) && (criteria.MaxPrice == null || a.AccommodationRates.Any(r => r.To criteria.Locations.Contains(j.Place.PlaceName))) ); 此查询的最后一行导致我出现问题 (criteria.Locations == null || criteria.Locations.Count == 0 || a.AccommodationPlaceJoins.Any(j => criteria.Locations.Contains(j.Place.PlaceName))) 它给出的错误是 无法创建类型为’System.Collections.Generic.IList`1’的常量值。 在此上下文中仅支持基本类型(例如Int32,String和Guid’)。 我甚至没有尝试创建列表。 我在这里尝试做的就是带回与一个地方相关的住宿(Place表中通过AccommodationPlaceJoin表链接到住宿表的地名)等于标准中的任何一个地名.Locations(属于IList类型)。 我已经尝试将此行更改为此,但它不起作用。 (criteria.Locations == null || criteria.Locations.Count == 0 || a.AccommodationPlaceJoins.Any(j => criteria.Locations.Any(l […]

如何在Dynamic LINQ中使用Contains时动态键入Cast到字符串?

我想使用动态LINQ查询来搜索类中所有属性中的一些文本。 我正在使用以下函数来创建表达式。 我正在将属性名称和搜索文本传递给方法。 在该方法中,如果属性类型是String,那么它工作正常。 如果属性类型是int,DateTime,GUID。 然后它不起作用。 我们知道Contains方法只用于元素数组或字符串。 我认为属性的值应该输入到字符串。 那怎么办呢? 带解释的解决方案是完整的。 我收集了这个代码。 public static Expression<Func> ContainsExp(string propertyName, string contains) { var parameterExp = Expression.Parameter(typeof(T), “type”); var propertyExp = Expression.Property(parameterExp, propertyName); MethodInfo method = typeof(string).GetMethod(“Contains”, new[] { typeof(string) }); var someValue = Expression.Constant(contains, typeof(string)); var containsMethodExp = Expression.Call(propertyExp, method, someValue); return Expression.Lambda<Func>(containsMethodExp, parameterExp); }