简体   繁体   English

LINQ to SQL查询的问题

[英]Issues With LINQ to SQL Query

I am having trouble getting one of my LINQ to SQL queries to work correctly. 我无法让我的LINQ to SQL查询之一正常工作。 I have three tables setup as follows: 我有以下三个表设置:

Vendors id - primary key 供应商ID-主键

Manufacturers id - primary key 厂商ID-主键

ManufacturerVendorRelationships id - primary key manufacturer_id - foreign key to manufacturer.id vendor_id - foreign key to vendor.id ManufacturerVendorRelationships id-主键maker_id-Manufacturer.id的外键vendor_id-vendor.id的外键

I am trying to write a LINQ to SQL query that will fetch the Vendors that are not currently related to a given manufacturer. 我正在尝试编写LINQ to SQL查询,该查询将获取当前与给定制造商不相关的供应商。 For example, the following table data should only result in 2 vendors for the manufacturer with ID of 1. Can someone please help me with the LINQ to SQL for this example? 例如,下表数据应仅导致ID为1的制造商的2个供应商。在此示例中,有人可以帮助我使用LINQ to SQL吗? Right now, the application is doing this logic. 现在,应用程序正在执行此逻辑。 Thanks in advance. 提前致谢。

Vendors
ID
1
2
3
4
5

Manufacturers
ID
1
2
3
4
5

ManufacturerVendorRelationships
ID               ManufacturerID                   VendorID
1                1                                1
2                1                                2
3                1                                3

Maybe something like this: 也许是这样的:

var result=(
        from v in db.Vendors
        where !db.ManufacturerVendorRelationships
                 .Select(s=>s.VendorID)
                 .Contains(v.ID)
        select v
    );

Or if you want it like a field: 或者,如果您希望将其像字段一样:

var result=(
        from v in db.Vendors
        select  new
        {
            v.ID,
            HasManufacturer=db.ManufacturerVendorRelationships
                           .Select(s=>s.VendorID)
                           .Contains(v.ID)
        }
    );

where db is the linq data context 其中db是linq数据上下文

int id= 1;
var result = 
dc.Vendors
  .Where(v => !dc.ManufacturerVendorRelationships
                 .Any(rel => rel.VendorId == v.Id && rel.ManufacturerId == id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM