简体   繁体   English

从Linq2Sql中的3个表中提取记录

[英]Pull out records from 3 tables in Linq2Sql

I have got 3 tables in my database. 我的数据库中有3个表。 Company, Product and Lead. 公司,产品和潜在客户。 Below are my fields. 以下是我的领域。

Leads Table: 潜在客户表:

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

Products Table: 产品表:

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

I have one more table " Company " whose reference is shown above in the two tables. 我还有一个表“ Company ”,上面的参考在两个表中显示。 My requirement is to pull out all companies that exists in above tables, Products and BuySell in same categories. 我的要求是淘汰以上表中存在的所有公司,产品和BuySell的同一类别。 For example If I want to see all companies of CategoryId = 15, then it will pull all companies with categoryid= 15 from buysell and products table. 例如,如果我想查看CategoryId = 15的所有公司,则它将从buysell和products表中拉出categoryid = 15的所有公司。 Obviously there will be redundancy, so i will use Distinct() to extract distinct items. 显然会有冗余,因此我将使用Distinct()提取不同的项目。

My Linq2Sql Biz layer method 我的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();
        }

But the above code return me 0 items. 但是上面的代码返回了我0个项目。 When i design its sql , see below, 当我设计其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)

i am able to get the company from BuySell but not from Product. 我可以从BuySell获得公司,但不能从Product获得公司。 So please help me. 所以请帮帮我。 I need the LINQ equivalent statement. To replace the above cs code.

If you have any relation between those tables, the query might be much simpler 如果这些表之间有任何关系,查询可能会简单得多

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

The query used with SSMS checks for CategoryId = 1 and uses an AND condition, but your LINQ query uses an OR condition 与SSMS一起使用的查询检查CategoryId = 1并使用AND条件,但是您的LINQ查询使用OR条件

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

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