标签: linq-to-entities

ASP.NET MVC 搜索页面 - Linq + EF4 上的整数开头

因此,在我的上一篇文章中,我询问如何使用 LINQ 和 EF4 构建动态搜索过滤器(请参阅此处),最后提出了将表达式构建为字符串并使用动态 LINQ 库将其解析为表达式的解决方案。

我解决了这个问题。我能够生成 aExpression<Func<TSource, out bool>>并将其传递给Where()的方法DbSet。我还尝试使用 MySql 作为 EF4 背后的数据库来做到这一点。

当我尝试对整数应用字符串操作时,问题就出现了,比如搜索连续数字以 1234 开头的数据库记录。

我最初的表情是这样的:record.ConsecutiveNumber.ToString().StartsWith("1234")。遗憾的是,正如预期的那样,事情并不那么容易,因为 EF4 无法查询DbSet异常:

“LINQ to Entities 无法识别‘System.String ToString()’方法,并且该方法无法转换为存储表达式。”

经过一番谷歌搜索后,我发现这是一个常见问题。但是来吧!有没有办法执行搜索功能,可以搜索以“1234”开头的连续编号的记录?

专业人士如何使用 EF4 实现搜索功能?这是使用单个属性过滤器的情况。如果我想添加多个过滤器怎么办?天哪,我的头好痛……:/

谢谢!

编辑:

想法#1:存储过程怎么样?从 Linq 调用 MySql 存储过程怎么样?我的目标是否太高了?

c# mysql linq linq-to-entities entity-framework

1
推荐指数
1
解决办法
3834
查看次数

实体框架代码优先重写 onModelCreating() 以实现 TPT 继承搞砸身份用户和角色模型

我在将 Fluent API 扩展到我的继承类时遇到问题。我采用了TPT(table per type)方法,每个类型的继承都有一个表。我喜欢每种类型的表,因为数据库完全规范化并且易于维护。我在覆盖本文指示我执行的onModelCreating方法时遇到错误。它正在丢失用户和角色实体的密钥。

基础抽象类

namespace Home_Warranty.Models
{
    public abstract class Vendor
    {
        [Key]
        public int VendorID { get; set; }

        [Required]
        public string CompanyName { get; set; }

        [Required]
        public int StreetNumber { get; set; }

        [Required]
        public string StreetName { get; set; }

        }
}
Run Code Online (Sandbox Code Playgroud)

从供应商继承的 ServiceCompany 类

namespace Home_Warranty.Models
 {
 [Table("ServiceCompanies")]
 public class ServiceCompany : Vendor
 {
    public string ACHClaim { get; set; }

    public virtual ICollection<SubContractorCompany> SubContractorCompanies{ get; set; } …
Run Code Online (Sandbox Code Playgroud)

linq inheritance linq-to-entities entity-framework entity-framework-migrations

1
推荐指数
1
解决办法
7500
查看次数

具有自定义方法的 LINQ to Entities Where 子句

我正在使用 Entity Framework Core (2.0) 并且我有以下疑问。

我不确定这样做时会发生什么:

context.Customers.Where(c => MyCustomMethod(c));

bool MyCustomMethod(Customer c) 
{
    return c.Name.StartsWith("Something");
}
Run Code Online (Sandbox Code Playgroud)

它是否可以毫无问题地转换为 SQL?

它与写作不同吗:

context.Customers.Where(c => c.StartsWith("Something"));
Run Code Online (Sandbox Code Playgroud)

简而言之,我能否将我对 Where 类的验证封装在方法中?它会破坏到 SQL 的转换吗?

.net c# linq-to-entities entity-framework entity-framework-core

1
推荐指数
1
解决办法
1218
查看次数

创建一个不区分大小写的 iQueryable,用于对实体进行单元测试 - NSubstitute

我正在尝试对 Linq 到实体进行单元测试,我想在不同的大小写中搜索相同的单词并返回相同的单词。

当前的情况是我正在尝试对小写和大写单词进行单元测试,例如“Hi”和“hi”。

使用实体框架的 Linq to 实体当前支持此功能,我可以在 where 子句中搜索这两个术语,它为我工作。

问题:我正在尝试制作一个行为相同的模拟可查询:

 public class SimpleWord
    {
        public string Text;
    }

    [Test]
    public void someTest()
    {
        //arrange
        var lowerWords = new[] { "hi" };
        var upperWords = new[] { "Hi" };

        var wordsList = new List<SimpleWord> {new SimpleWord { Text = "hi" } };
        IDbSet<SimpleWord> wordsDbSet = Substitute.For<DbSet<SimpleWord>, IDbSet<SimpleWord>>();

        //set up the mock dbSet
        var dataAsList = wordsList.ToList();
        var queryable = dataAsList.AsQueryable();
        wordsDbSet.Provider.Returns(queryable.Provider);
        wordsDbSet.Expression.Returns(queryable.Expression);
        wordsDbSet.ElementType.Returns(queryable.ElementType);
        wordsDbSet.GetEnumerator().Returns(queryable.GetEnumerator());

        //act
        var resultLower = wordsDbSet.Where(wrd …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-entities linq-to-sql nsubstitute

1
推荐指数
1
解决办法
980
查看次数

添加“跳过”引发异常:方法“跳过”仅支持 LINQ to Entities 中的排序输入

我试图在跳过 x 组的同时获取 n 组的所有行,因此我不必一次执行所有记录。通过这种方式,我可以实现分页以更快地加载数据。

下面的代码工作正常(没有跳过),我从前 2 个组中获取所有记录(按相同的 Id 分组)。

IOrderedQueryable<Patient> query = query.OrderBy(r => r.Id);

IQueryable<Patient> groupedQuery = query.GroupBy(x => x.Id).Take(2).SelectMany(g => g);
Run Code Online (Sandbox Code Playgroud)

但是要实现分页,当我尝试按以下方式添加“跳过”并调用groupedQuery.ToList();.

IQueryable<Patient> groupedQuery = query.GroupBy(x => x.Id).Skip(2).Take(4).SelectMany(g => g);
Run Code Online (Sandbox Code Playgroud)

它抛出异常:

方法“跳过”仅支持 LINQ to Entities 中的排序输入。方法 'OrderBy' 必须在方法 'Skip' 之前调用。

有人可以建议正确的做法。

c# linq-to-entities entity-framework

1
推荐指数
1
解决办法
375
查看次数

如何检索ADO.NET实体中的一个数据库中的记录和所有子记录?

我想做一个数据库调用来检索以下数据,但我很难弄清楚LINQ应该是什么样子.这是我目前的实现(我知道它很糟糕!!):

var photos = from photo in entitiesContext.Photo
             join pg in entitiesContext.PhotoGallery on photo.PhotoGallery.PhotoGalleryID equals pg.PhotoGalleryID
             where pg.PhotoGallery == photoGalleryID
             select photo;

var photoList = photos.ToList();

foreach (var photoForLoading in photoList)
{
    photoForLoading.UserReference.Load();
    photoForLoading.PhotoComments.Load();

    foreach (var comment in photoForLoading.PhotoComment)
    {
        comment.UserReference.Load();
    }
}

return photoList;
Run Code Online (Sandbox Code Playgroud)

所以你可以看到我想要检索:

  • 来自特定照片库的所有照片:
    • 每张照片的用户详细信息
    • 每张照片的评论
      • 每条评论的用户详细信息

如何在LINQ中使用ADO.NET实体框架执行此操作?

干杯,灰.

c# linq ado.net linq-to-entities entity-framework

0
推荐指数
1
解决办法
711
查看次数

为什么LINQ-to-Entities生成的SQL效率低下?

以下(削减)代码摘录是LINQ到实体的查询导致SQL(经由ToTraceString),其是比手工制作的查询慢.我做了什么愚蠢的事情,还是Linq-to-Entities在优化查询方面做得不好?

我在查询结束时有一个ToList(),因为我需要在使用它来构建XML数据结构之前执行它(这是另一个痛苦).

var result = (from mainEntity in entities.Main
              where (mainEntity.Date >= today) && (mainEntity.Date <= tomorrow) && (!mainEntity.IsEnabled)
              select new
              {
                  Id = mainEntity.Id,
                  Sub =
                      from subEntity in mainEntity.Sub
                      select
                      {
                          Id = subEntity.Id,
                          FirstResults =
                              from firstResultEntity in subEntity.FirstResult
                              select new
                              {
                                  Value = firstResultEntity.Value,
                              },
                          SecondResults =
                              from secondResultEntity in subEntity.SecondResult
                              select
                              {
                                  Value = secondResultEntity.Value,
                              },
                          SubSub =
                              from subSubEntity in entities.SubSub
                              where (subEntity.Id == subSubEntity.MainId) && (subEntity.Id == subSubEntity.SubId)
                              select
                                  new
                                  { …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-entities

0
推荐指数
1
解决办法
558
查看次数

自定义通用GetTable

对于我正在处理的DataContext,我不想加载表直到需要它们.我想创建一个方法来检查某个表是否在加载之前加载,但我最终有n个方法做同样的事情:

private void Load(ref Table<Order> Orders)
{
    if (Orders == null)
        Orders = this.GetTable<Order>();
}
Run Code Online (Sandbox Code Playgroud)

我试图制作一个通用的,原因很明显,但我得到一个"类型'T'必须是一个引用类型,以便在泛型类型或方法'System.Data.Linq中将它用作参数'TEntity' .Table'"这样做的例外情况:

private void Load<T>(ref Table<T> TableToLoad)
{
    if (TableToLoad == null)
        TableToLoad = this.GetTable<T>();
}
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-entities

0
推荐指数
1
解决办法
1062
查看次数

LINQ to Entities和分组与多对多关联

我在多对多关联中有一个产品和类别实体.我正在尝试显示每种产品的类别数量.到目前为止,我已经想出了这个:

Dim context = new Context()
Dim products = context.Products()
Dim productsByCategoryCount = From product In Products
 Group product By productId = product.productId Into productCount =  Count(product.cateogories.Count)
Run Code Online (Sandbox Code Playgroud)

查询执行但未显示正确的结果.我究竟做错了什么?

linq-to-entities many-to-many entity-framework group-by

0
推荐指数
1
解决办法
783
查看次数

在VS 2008中编译预生成视图时出错

我使用VS 2008,.NET 3.5和Entity框架为我的EDMX创建了预生成的视图.

我使用这里提到的t4模板生成了我的mymodel.views.cs文件.它的大小约为40 mb.

将views.cs文件添加到我的Web应用程序和我的Web服务中.当我构建应用程序时,webservice将构建没有任何错误,我可以让WS工作.但是,网站中的构建失败并出现错误

错误CS0234:命名空间"System.Data"中不存在类型或命名空间名称"Mapping"(您是否缺少程序集引用?)

两个项目都引用了System.Data程序集,版本表示.NET 2.0.怀疑我在这里失踪的一些微不足道的东西,但仍然无能为力.

任何指针都会有所帮助!

linq-to-entities entity-framework visual-studio-2008

0
推荐指数
1
解决办法
792
查看次数