我正在将一些东西从一个mysql服务器迁移到一个sql server但我无法弄清楚如何使这个代码工作:
using (var context = new Context())
{
...
foreach (var item in collection)
{
IQueryable<entity> pages = from p in context.pages
where p.Serial == item.Key.ToString()
select p;
foreach (var page in pages)
{
DataManager.AddPageToDocument(page, item.Value);
}
}
Console.WriteLine("Done!");
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
当它进入第二个时foreach (var page in pages)它抛出一个异常说:
LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为存储表达式.
谁知道为什么会这样?
我正在尝试使用LINQ to Entities构建搜索页面,但是下面的代码给出了一个运行时错误,关于lte没有识别'Boolean StartsWith().代码编译得很好.我怎样才能解决这个问题,而不是将StartsWith过滤到存储过程?
return from dp in dents.DirectoryPersonEntrySet
where
((dp.LastName.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
(dp.Department.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase)) ||
dp.Extension.StartsWith(searchTerm, StringComparison.CurrentCultureIgnoreCase))
select dp;
Run Code Online (Sandbox Code Playgroud) 我怎么解决这个问题?
这是我的代码:
DateTime dtInicio = new DateTime();
DateTime dtFim = new DateTime();
Int32 codStatus = 0;
if(!string.IsNullOrEmpty(collection["txtDtInicial"]))
dtInicio = Convert.ToDateTime(collection["txtDtInicial"]);
if(!string.IsNullOrEmpty(collection["txtDtFinal"]))
dtFim = Convert.ToDateTime(collection["txtDtFinal"]);
if (!string.IsNullOrEmpty(collection["StatusCliente"]))
Convert.ToInt32(collection["StatusCliente"]);
var listCLientResult = (from c in db.tbClientes
orderby c.id
where (c.effdt >= dtInicio || string.IsNullOrEmpty(collection["txtDtInicial"]) &&
(c.effdt <= dtFim || string.IsNullOrEmpty(collection["txtDtFinal"])) &&
(c.cod_status_viagem == codStatus || string.IsNullOrEmpty(collection["StatusCliente"])))
select c);
return View(listCLientResult);
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
LINQ to Entities无法识别方法'System.String get_Item(System.String)',该方法无法转换为存储库的表达式.
如何在实体框架中使用replace方法.我使用以下代码但遇到错误.
using (SepasProjectEntities sp=new SepasProjectEntities())
{
var query = (from p in sp.HISAccidentLocationMappings
where p.Name.Replace('y','x').Contains(txt1.Text)
select p
).ToList();
}
Run Code Online (Sandbox Code Playgroud)
System.Data.Entity.dll中出现"System.NotSupportedException"类型的异常,但未在用户代码中处理
附加信息:LINQ to Entities无法识别方法'System.String Replace(Char,Char)'方法,并且此方法无法转换为存储表达式.
我在lambda语法中有以下linq表达式:
var myValue = 6;
var from = 2;
var to = 8;
var res = MyList.Where(m => m.person.Id == person.Id
&& IsBetween(myValue, from, to))
.Select(x => new Person { blah blah blah })
.ToList());
Run Code Online (Sandbox Code Playgroud)
IsBetween是一个简单的通用助手方法,看看我之间是否有东西:
public bool IsBetween<T>(T element, T start, T end)
{
return Comparer<T>.Default.Compare(element, start) >= 0
&& Comparer<T>.Default.Compare(element, end) <= 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我收到了这个错误,我不知道如何解决它:
LINQ to Entities无法识别方法'Boolean IsBetween [Decimal](System.Decimal,System.Decimal,System.Decimal)'方法,并且此方法无法转换为商店表达式.
我有以下代码使用 EF 获取所有数据,然后尝试将它们转换为模型,如下所示。
var patients = allpatients.Select(p => CreatePatient(p));
public Patient CreatePatient(PATIENT p)
{
Patient patient = new Patient();
patient.FIRSTNAME = p.FIRSTNAME;
patient.MIDDLENAME = p.MIDDLENAME;
patient.SURNAME = p.SURNAME;
return patient;
}
Run Code Online (Sandbox Code Playgroud)
但是得到这个错误
“LINQ to Entities 无法识别方法 'Model.Patient CreatePatient(Repository.PATIENT)' 方法,并且此方法无法转换为存储表达式。”
有时您需要定义一些业务规则,规范模式是一个有用的工具。例如:
public class CanBorrowBooksSpec : ISpecification<Customer>
{
public bool Satisfies(Customer customer)
{
return customer.HasLibraryCard
&& !customer.UnpaidFines.Any();
}
}
Run Code Online (Sandbox Code Playgroud)
然而,我经常发现我需要将这些规则“推送”到 SQL 中以提高性能或满足记录分页列表之类的要求。
然后,我必须为规则编写代码两次,一次使用 CLR 代码,一次使用 SQL(或 ORM 语言)。
您如何组织这样的代码?
最好将代码放在同一个类中。这样,如果开发人员正在更新业务规则,他们忘记更新两组代码的可能性就较小。例如:
public class CanBorrowBooksSpec : ISpecification<Customer>
{
public bool Satisfies(Customer customer)
{
return customer.HasLibraryCard
&& !customer.UnpaidFines.Any();
}
public void AddSql(StringBuilder sql)
{
sql.Append(@"customer.HasLibraryCard
AND NOT EXISTS (SELECT Id FROM CustomerUnpaidFines WHERE CustomerId = customer.Id)");
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这对我来说似乎很难看,因为我们现在将担忧混合在一起。
另一种替代方案是使用 Linq-To-YourORM 解决方案,因为 LINQ 代码可以针对集合运行,也可以转换为 SQL。但我发现,除了最琐碎的场景之外,这种解决方案几乎不可能实现。
你做什么工作?
我有一堂这样的课:
public class menu{
public string Permission{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
的值Permission是 Encripted 的。我想要所有记录在Permission哪里True。为此,我使用以下查询:
return
_menuSettings.Where(row => Convert.ToBoolean(Utilities.Encryption.Decrypt(row.Permission,"key"))==true).ToList();
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
LINQ to Entities 无法识别“Boolean ToBoolean(System.String)”方法,并且此方法无法转换为存储表达式。
我在google上搜索过,但无法解决。
谢谢
这是另一个"LINQ to实体无法识别方法问题"......但是,下面的代码不是从根本上做同样的事情吗?
作品:
var returnData = from x in MyEntities.MyDBSet
where x.MyDBSetPrimaryKey == id
select new Models.MyModelDTO
{
MyPropOne = (int)x.MyModel.MyOtherPropOne,
MyPropTwo = x.MyOtherPropTwo ?? 0,
MyPropThree = x.MyModel.MyOtherPropThree,
MyPropFour = x.MyModel.MyOtherPropFour,
MyPropFive = x.MyModel.Entity.MyOtherPropFive,
MyPropSix = x.MyModel.MyOtherPropSix == null ? 0 : (decimal)x.MyModel.MyOtherPropSix,
MyPropSeven = x.MyModel.SomeType.MyOtherPropSeven,
MyPropEight = (int)x.MyModel.MyOtherPropEight,
MyPropNine = x.MyModel.MyPropNine == null ? 0 : (int)x.MyModel.MyOtherPropNine,
MyPropTen = x.MyModel.MyOtherPropTen == null ? 0 : (int)x.MyModel.MyOtherPropTen,
MyPropEleven = x.OtherEntity.MyOtherPropEleven,
MyPropTwelve = x.MyOtherpropTwelve
};
Run Code Online (Sandbox Code Playgroud)
不起作用:
包含在扩展方法中的完全相同的赋值:
public static MyModelDTO …Run Code Online (Sandbox Code Playgroud) 这是我获取数据的代码.
var list = (from u in _dbContext.Users
where u.IsActive
&& u.IsVisible
&& u.IsPuller.HasValue
&& u.IsPuller.Value
select new PartsPullerUsers
{
AvatarCroppedAbsolutePath = u.AvatarCroppedAbsolutePath,
Bio = u.Bio,
CreateDateTime = u.CreationDate,
Id = u.Id,
ModifieDateTime = u.LastModificationDate,
ReviewCount = u.ReviewsReceived.Count(review => review.IsActive && review.IsVisible),
UserName = u.UserName,
Locations = (from ul in _dbContext.UserLocationRelationships
join l in _dbContext.Locations on ul.LocationId equals l.Id
where ul.IsActive && ul.UserId == u.Id
select new PartsPullerLocation
{
LocationId = ul.LocationId,
Name = ul.Location.Name
}),
Rating = u.GetPullerRating()
}); …Run Code Online (Sandbox Code Playgroud)