【问题标题】:Pull out records from 3 tables in Linq2Sql从 Linq2Sql 中的 3 个表中提取记录
【发布时间】:2012-04-25 17:47:11
【问题描述】:

我的数据库中有 3 个表。公司、产品和潜在客户。以下是我的领域。

线索表:

SELECT [Id]
      ,[BuySellTypeId]
      ,[ProductName]
      ,[Keywords]
      ,[CategoryId] // categoryid
      ,[SubCategoryId]
      ,[Description]
      ,[ProductImagePath]
      ,[CreationDate]
      ,[ExpiryDate]
      ,[CompanyId] // company id
      ,[ShortDescription]
      ,[IsExpired]
      ,[IsActive]
  FROM [BuySell]

产品表:

SELECT   
        Id, 
        Name, 
        Description, 
        ImagePath, 
        CompanyId, //company id
        Keywords, 
        CategoryId,  // categoryid
        SubCategoryId, 
        PostedDate, 
        ExpiryDate, 
        ShortDescription, 
        IsActive,                       
        IsExpired
FROM  Products

我还有一张表“Company”,上面两张表中显示了它的引用。我的要求是提取上表中存在的所有公司,Products 和 BuySell 属于同一类别。 例如,如果我想查看 CategoryId = 15 的所有公司,那么它将从 buysell 和 products 表中拉出所有 categoryid = 15 的公司。显然会有冗余,所以我将使用Distinct() 来提取不同的项目。

我的 Linq2Sql Biz 层方法

        /// <summary>
        /// Returns all companies by category id that exists in Lead and Products
        ///table
        /// </summary>
        /// <param name="categoryId">category id as integer</param>
        /// <param name="take">records to take</param>
        /// <param name="skip">records to skip</param>
        /// <returns>List of companies</returns>
        public static IList GetCompaniesByCategory(int categoryId, int take, int skip)
        {
            return (from c in Context.Companies
                   join bs in Context.BuySells on c.CompanyId equals bs.CompanyId
                   join p in Context.Products on c.CompanyId equals p.CompanyId
                   where bs.CategoryId == categoryId || p.CategoryId==categoryId
                   select new
                              {
                                  c.CompanyId,
                                  c.CompanyName,
                                  c.Country.Flag,
                                  c.Profile,
                                  c.IsUsingSMSNotifications,
                                  c.IsVerified,
                                  c.MembershipType,
                                  c.RegistrationDate, c.ShortProfile,
                              }).Skip(skip).Take(take).Distinct().ToList();
        }

但上面的代码返回了 0 个项目。当我设计它的 sql 时,见下文,

SELECT     dbo.Company.CompanyId, dbo.Company.CompanyName, dbo.Company.Name, dbo.Company.IsVerified, dbo.Company.IsUsingSMSNotifications, 
                      dbo.Company.CompanyLogo, dbo.Company.RegistrationDate, dbo.Company.Profile
FROM         dbo.BuySell INNER JOIN
                      dbo.Company ON dbo.BuySell.CompanyId = dbo.Company.CompanyId LEFT OUTER JOIN
                      dbo.Products ON dbo.Company.CompanyId = dbo.Products.CompanyId
WHERE     (dbo.BuySell.CategoryId = 1) AND (dbo.Products.CategoryId = 1)

我可以从 BuySell 获得公司,但不能从 Product 获得。所以请帮助我。 I need the LINQ equivalent statement. To replace the above cs code.

【问题讨论】:

  • 我不认为这个 SQL 是从那个查询生成的。 company 表与 buysells 和 products 表之间有什么关系吗?
  • 1. Sql 是在 Management Studio 中设计的,不是 Linq 生成的。 2. 是的,他们确实有关系,请参阅上面的表格和注释行......

标签: c# sql linq sql-server-2008 linq-to-sql


【解决方案1】:

如果您在这些表之间有任何关系,则查询可能会简单得多

return (from c in Context.Companies
where c.BuySells.Any( b=>b.CategoryId == categoryId)
      || c.Products.Any( p=>p.CategoryId == categoryId)
select new
{
  c.CompanyId,
  c.CompanyName,
  c.Country.Flag,
  c.Profile,
  c.IsUsingSMSNotifications,
  c.IsVerified,
  c.MembershipType,
  c.RegistrationDate, c.ShortProfile,
}).Skip(skip).Take(take).ToList();

与 SSMS 一起使用的查询检查 CategoryId = 1 并使用 AND 条件,但您的 LINQ 查询使用 OR 条件

【讨论】:

  • 一件事“LINQ 查询使用 OR 条件”,这是什么意思,我可以看到您在 where 条件下使用了 &&。请详细说明。
  • 抱歉,您的 LINQ 查询使用了这个
  • 所以你想说我必须使用 && 而不是 ||。还有一次,我想澄清一下,我需要一张或两张桌子上的公司。如果它只存在于产品中,那么它也存在,如果它只存在于潜在客户中,那么它也存在于两个产品中,如果存在于两个产品中,那么来自两个表。所以在那种情况下 && 是合适的或 ||将被应用
猜你喜欢
  • 1970-01-01
  • 2015-09-18
  • 2021-04-26
  • 1970-01-01
  • 2010-09-09
  • 2010-12-31
  • 2021-12-14
  • 2012-10-18
  • 1970-01-01
相关资源
最近更新 更多