0

我的数据库中有 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

我还有一个表“公司”,其参考在上面的两个表中显示。我的要求是提取上表中存在的所有公司,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.

4

1 回答 1

0

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

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 条件

于 2012-04-13T11:25:41.747 回答