I have got 3 tables in my database. Company, Product and Lead. Below are my fields.
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
I have one more table " Company " whose reference is shown above in the two tables. My requirement is to pull out all companies that exists in above tables, Products and BuySell in same categories. 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. Obviously there will be redundancy, so i will use Distinct()
to extract distinct items.
My Linq2Sql Biz layer method
/// <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. When i design its sql , see below,
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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.