Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EF中的子查询编写T-SQL多对多_C#_Sql Server_Entity Framework_Linq_Entity Framework 6 - Fatal编程技术网

C# 如何使用EF中的子查询编写T-SQL多对多

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

在ASP.NET EF应用程序中,我有两个具有多对多关系的类。我正在尝试查找从视图发布的所有
列表
,其中包含任何
类别
。类别是视图窗体上的复选框

这些类简化了导航属性,例如:

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());