C# 如何使用EF中的子查询编写T-SQL多对多
在ASP.NET EF应用程序中,我有两个具有多对多关系的类。我正在尝试查找从视图发布的所有C# 如何使用EF中的子查询编写T-SQL多对多,c#,sql-server,entity-framework,linq,entity-framework-6,C#,Sql Server,Entity Framework,Linq,Entity Framework 6,在ASP.NET EF应用程序中,我有两个具有多对多关系的类。我正在尝试查找从视图发布的所有列表,其中包含任何类别。类别是视图窗体上的复选框 这些类简化了导航属性,例如: public class Listing { public int ID { get; set; } public ICollection<Category> Categories { get; set; } ... } public class Category { p
列表
,其中包含任何类别
。类别是视图窗体上的复选框
这些类简化了导航属性,例如:
public class Listing
{
public int ID { get; set; }
public ICollection<Category> Categories { get; set; }
...
}
public class Category
{
public int ID { get; set; }
public ICollection<Listing> Listings { get; set; }
...
}
// this is the join table created by EF code first for reference
public class CategoryListings
{
public int Category_ID { get; set; }
public int Listing_ID { get; set; }
}
在SQL中,我将使用子查询(子查询表示可以搜索的多个类别)编写此代码:
如何使用导航器或lambda在EF中编写SQL查询?SQL中的子查询将更改每次搜索,可以是任何id。您是否在
列表/类别
和分类列表
之间有关系?
以下是EF 6的示例:
如果有,查询将很简单,类似于:
CategoryListing.Where(cl=>newlist{1,3}.Contains(cl.CategoryRefId))
.Select(x=>new{x.ListingRefId,x.CategoryRefId})代码>
如果您需要列表
或类别
的所有属性,Include()
扩展将有帮助。您在列表/类别
和类别列表
之间有关系吗?
以下是EF 6的示例:
如果有,查询将很简单,类似于:
CategoryListing.Where(cl=>newlist{1,3}.Contains(cl.CategoryRefId))
.Select(x=>new{x.ListingRefId,x.CategoryRefId})代码>
如果您需要列表
或类别
的所有属性,Include()
扩展将有所帮助。您忘记告诉我们您的收藏项目中有哪些对象
。我认为它们是列表
。您的案例不起作用,因为itemsTemp
是类别的集合
,而每个item1
都是类别
,当然不能转换为列表
建议:要调试铸造问题,请替换单词var
和你期望的类型。编译器将警告您
类型不正确。在lambda表达式中也使用适当的标识符。
这使它们更容易阅读
以及DbContext
public MyDbContext : DbContext
{
public DbSet<Listing> Listings {get; set;}
public DbSet<Category> Categories {get; set;}
}
数据库查询中较慢的部分之一是将所选数据从DBMS传输到本地进程。因此,只转移实际计划使用的属性是明智的。例如,您不需要一对多关系的外键,您知道它等于一对多关系中one
部分的Id值
返回您的代码
在我看来,您的项目
是列表
。在这种情况下,您的代码需要所有列表
,其中至少有一个已启用的类别
var result = myDbContext.Listings
.Where(listing => ...) // only if you don't want all listings
.Select(listing => new
{
Id = listing.Id,
Name = list.Name,
Categories = listing.Categories
.Where(category => category.Enabled) // keep only the enabled categories
.Select(category => new
{
Id = category.Id,
Name = category.Name,
...
})
.ToList(),
})
// this will give you also the Listings that have only disabled categories,
// so listings that have any categories left. If you don't want them:
.Where(listing => listing.Categories.Any());
您忘了告诉我们您的收藏中有哪些对象项目
。我认为它们是列表
。您的案例不起作用,因为itemsTemp
是类别的集合
,而每个item1
都是类别
,当然不能转换为列表
建议:要调试铸造问题,请替换单词var
和你期望的类型。编译器将警告您
类型不正确。在lambda表达式中也使用适当的标识符。
这使它们更容易阅读
以及DbContext
public MyDbContext : DbContext
{
public DbSet<Listing> Listings {get; set;}
public DbSet<Category> Categories {get; set;}
}
数据库查询中较慢的部分之一是将所选数据从DBMS传输到本地进程。因此,只转移实际计划使用的属性是明智的。例如,您不需要一对多关系的外键,您知道它等于一对多关系中one
部分的Id值
返回您的代码
在我看来,您的项目
是列表
。在这种情况下,您的代码需要所有列表
,其中至少有一个已启用的类别
var result = myDbContext.Listings
.Where(listing => ...) // only if you don't want all listings
.Select(listing => new
{
Id = listing.Id,
Name = list.Name,
Categories = listing.Categories
.Where(category => category.Enabled) // keep only the enabled categories
.Select(category => new
{
Id = category.Id,
Name = category.Name,
...
})
.ToList(),
})
// this will give you also the Listings that have only disabled categories,
// so listings that have any categories left. If you don't want them:
.Where(listing => listing.Categories.Any());
您是否尝试过嵌套任何s?类似于Listing.Categories.Any(x=>listIds.Any(y=>x.Id=y))
。@BagusTesa我使用var-selectedItems=listings.Any(x=>model.Categories.Any(y=>x.Id==y.Id))尝试了这个方法代码>它只返回true
。我需要一个列表实体的集合,我怎样才能得到它呢?啊,snap,应该是listing.Categories.Where(x=>listIds.Any(y=>x.Id=y))
我不应该在没有完全清醒的时候写建议。。请注意,listIds
是要获取的类别ID的列表。listIds.Any(y=>x.Id==y)
将在(从Id=1或Id=3的类别中选择Id)中转换为x.Id
。也许我没有正确阅读它,但您的示例来自一个列表
对吗?我的示例将从列表的集合中调用,那么这是如何工作的呢?类似于IEnumerable listings=“all listings”代码>`listings.Categories.Where(x=>listIds.Any(y=>x.Id=y))dbContext.Listing.Where(x=>listIds.Any(y=>x.Categories.Id=y))
my Bad您尝试过嵌套Any
s吗?类似于Listing.Categories.Any(x=>listIds.Any(y=>x.Id=y))
。@BagusTesa我使用var-selectedItems=listings.Any(x=>model.Categories.Any(y=>x.Id==y.Id))尝试了这个方法代码>它只返回true
。我需要一个列表实体的集合,我怎样才能得到它呢?啊,snap,应该是listing.Categories.Where(x=>listIds.Any(y=>x.Id=y))
我不应该在没有完全清醒的时候写建议。。请注意,listIds
是要获取的类别ID的列表。listIds.Any(y=>x.Id==y)
将在(从Id=1或Id=3的类别中选择Id)中转换为x.Id
。可能我没有正确阅读它,但您的示例来自一个Listi
public MyDbContext : DbContext
{
public DbSet<Listing> Listings {get; set;}
public DbSet<Category> Categories {get; set;}
}
var result = myDbcontext.Listings
.Select(listing => new
{ // select only the properties you plan to use
Id = listing.Id,
Name = listing.Name,
...
Categories = listing.Categories
// you don't want all categories, you only want categories with id 1 or 3
.Where(category => category.Id == 1 || category.Id == 3)
.Select(category => new
{
// again select only the properties you plan to use
Id = category.Id,
Enabled = category.Enabled,
...
})
.ToList(),
})
// this will also give you the Listings without such Categories,
// you only want Listings that have any Categories left
.Where(listing => listing.Categories.Any());
var result = myDbContext.Listings
.Where(listing => ...) // only if you don't want all listings
.Select(listing => new
{
Id = listing.Id,
Name = list.Name,
Categories = listing.Categories
.Where(category => category.Enabled) // keep only the enabled categories
.Select(category => new
{
Id = category.Id,
Name = category.Name,
...
})
.ToList(),
})
// this will give you also the Listings that have only disabled categories,
// so listings that have any categories left. If you don't want them:
.Where(listing => listing.Categories.Any());