Merging two tables using criteria with linq - c#

Im trying to resolve something with just one linq sentence, and I dont know if is possible do this.
I have one table named PRICES, with this fields:
pkey: int
region: int?
product_type: int
product_size: int
price: double
desc: string
The unique key is: product_type + product_size
I want to do a query that returns all rows WHERE region == 17
(this is my first set of rows)
AND want to add all rows where region is null
(this is my second set of rows)
BUT
if there are rows with the same product_type and product_size in both sets, i want in the final result just the row of the first set.
Example:
pkey | region | product_type | product_size | price | desc
1, null, 20, 7, 2.70, salad1
2, null, 20, 3, 2.50, salad7
3, 17, 20, 7, 1.90, saladspecial
4, 17, 20, 5, 2.20, other
I want a linq query that returns this:
2, null, 20, 3, 2.50, salad7
3, 17, 20, 7, 1.90, saladspecial
4, 17, 20, 5, 2.20, other
(note that row with pkey 1 is discarded because the row with pkey 3 has the same product_type and product_size)
var query1 = from p in PRICES where p.region == 17
select p;
var query2 = from p in PRICES where p.region is null
select p;
Questions:
How to join query1 and query2 to obtain the expected output?
It can be done with just 1 query?

Following query selects only prices with region 17 or null, groups them by unique key { p.product_type, p.product_size }. Then it checks whether group contain at least one price with region 17. If yes, then we select all prices of this region from group (and skipping prices with null region). Otherwise we return whole group (it has null regions only):
var query = from p in PRICES.Where(x => x.region == 17 || x.region == null)
group p by new { p.product_type, p.product_size } into g
from pp in g.Any(x => x.region == 17) ?
g.Where(x => x.region == 17) : g
select pp;
Input:
1 null 20 7 2.7 salad1 // goes to group {20,7} with region 17 price
2 null 20 3 2.5 salad7 // goes to group {20,3} without region 17 prices
3 17 20 7 1.9 saladspecial // goes to group {20,7}
4 17 20 5 2.2 other // goes to group {20,5}
Output:
2 null 20 3 2.5 salad7
3 17 20 7 1.9 saladspecial
4 17 20 5 2.2 other
EDIT Query above works fine with objects in memory (i.e. LINQ to Objects) but LINQ to Entitis is not that powerful - it does not support nested queries. So, for Entity Framework you will need two queries - one to fetch prices with null region, which does not have region 17 prices in the group, and second - prices from region 17:
var pricesWithoutRegion =
db.PRICES.Where(p => p.region == 17 || p.region == null)
.GroupBy(p => new { p.product_type, p.product_size })
.Where(g => !g.Any(p => p.region == 17))
.SelectMany(g => g);
var query = db.PRICES.Where(p => p.region == 17).Concat(pricesWithoutRegion);
Actually EF executes both sub-queries in one UNION query to server. Following SQL will be generated (I removed desc and price columns to fit screen):
SELECT [UnionAll1].[pkey] AS [C1],
[UnionAll1].[region] AS [C2],
[UnionAll1].[product_type] AS [C3],
[UnionAll1].[product_size] AS [C4]
FROM (SELECT [Extent1].[pkey] AS [pkey],
[Extent1].[region] AS [region],
[Extent1].[product_type] AS [product_type],
[Extent1].[product_size] AS [product_size]
FROM [dbo].[Prices] AS [Extent1] WHERE 17 = [Extent1].[region]
UNION ALL
SELECT [Extent4].[pkey] AS [pkey],
[Extent4].[region] AS [region],
[Extent4].[product_type] AS [product_type],
[Extent4].[product_size] AS [product_size]
FROM (SELECT DISTINCT [Extent2].[product_type] AS [product_type],
[Extent2].[product_size] AS [product_size]
FROM [dbo].[Prices] AS [Extent2]
WHERE ([Extent2].[region] = 17 OR [Extent2].[region] IS NULL) AND
(NOT EXISTS
(SELECT 1 AS [C1] FROM [dbo].[Prices] AS [Extent3]
WHERE ([Extent3].[region] = 17 OR [Extent3].[region] IS NULL)
AND ([Extent2].[product_type] = [Extent3].[product_type])
AND ([Extent2].[product_size] = [Extent3].[product_size])
AND (17 = [Extent3].[region])
))) AS [Distinct1]
INNER JOIN [dbo].[Prices] AS [Extent4]
ON ([Extent4].[region] = 17 OR [Extent4].[region] IS NULL)
AND ([Distinct1].[product_type] = [Extent4].[product_type])
AND ([Distinct1].[product_size] = [Extent4].[product_size]))
AS [UnionAll1]
BTW it's surprise to me that GroupBy was translated into inner join with conditions.

I think you should go for 1 query, for 2 queries, we have to repeat something:
//for 2 queries
var query = query1.Union(query2.Except(query2.Where(x=>query1.Any(y=>x.product_type==y.product_type&&x.product_size==y.product_size))))
.OrderBy(x=>x.pkey);
//for 1 query
//the class/type to make the group key
public class GroupKey
{
public int ProductType { get; set; }
public int ProductSize { get; set; }
public override bool Equals(object obj)
{
GroupKey gk = obj as GroupKey;
return ProductType == gk.ProductType && ProductSize == gk.ProductSize;
}
public override int GetHashCode()
{
return ProductSize ^ ProductType;
}
}
//-------
var query = list.Where(x => x.region == 17 || x.region == null)
.GroupBy(x => new GroupKey{ProductType = x.product_type, ProductSize = x.product_size })
.SelectMany<IGrouping<GroupKey,Price>,Price,Price>(x => x.Where(k => x.Count(y => y.region == 17) == 0 || k.region == 17), (x,g) => g)
.OrderBy(x=>x.pkey);

Related

How to get dictinct userid with order by close using Linq

I have table called info, and its data something like this:
FKUserID
CompletedTime
Type
1
2021-03-10 12:56:00.423
5
245
2021-03-10 12:46:46.977
5
1
2021-03-10 12:44:53.683
5
1
2021-03-10 12:40:54.733
5
1
2021-03-10 12:35:26.307
5
245
2021-03-10 11:11:33.887
5
245
2021-03-10 10:18:11.403
5
I need to get distinct userID data, and also with the maximum completed time of theirs CompletedTime column
expected output is:
FKUserID
CompletedTime
Type
1
2021-03-10 12:56:00.423
5
245
2021-03-10 12:46:46.977
5
I need to do this using Linq query
How can I do this, I did it using SQl, need it using Linq
SELECT FKUserID , MAX(CompletedTime)
from Info
where cast(CompletedTime as date) = '2021-03-10'
and Status = 5
GROUP BY FKUserID;
You can use the following query. The following query implements your SQL query.
var query = context.Info.GroupBy(a => a.FKUserID)
.Join(context.Info,
left => left.Key,
right => right.FKUserID,
(left, right) => new { left, right })
.Where(a => a.right.CompletedTime.ToShortDateString() == "2021-03-10" && a.right.Status == 5)
.Select(a => new
{
FKUserID = a.left.Key,
CompletedTime = a.left.Max(x => x.CompletedTime)
}).ToList();
Something like this
var d = new DateTime(2021, 3, 10);
var d2 = d.AddDays(1);
dbContext.Infos
.Where(i => i.Status == 5 && i.CompletedTime >= d && i.CompletedTime < d2)
.GroupBy(i => i.FkUserId)
.Select(g => new {
FkUserId = g.Key,
CompletedTime = g.Max(g2 => g2.CompletedTime)
}
);

C# SQL to Linq - translation

I have a table with transactions, similar to:
--REQUEST_ID----ITEM_ID----ITEM_STATUS_CD----EXECUTION_DTTM
1 1 1 2016-08-29 12:36:07.000
1 2 0 2016-08-29 12:37:07.000
2 3 5 2016-08-29 13:37:07.000
2 4 1 2016-08-29 15:37:07.000
2 5 10 2016-08-29 15:41:07.000
3 6 0 2016-08-29 15:41:07.000
What i want is at table showing how many success/warning/Error in % with endtime of the latest transaction in the Request_ID:
--REQUEST_ID--Transactions----------EndTime----------Success----Warning----Error
1 2 2016-08-29 12:37:07.000 50 50 0
2 3 2016-08-29 15:41:07.000 0 33 66
3 1 2016-08-29 15:41:07.000 100 0 0
I have the table that I want by the following slq, but I dont know how to do it in linq(C#)....Anyone?
SELECT distinct t1.[REQUEST_ID],
t2.Transactions,
t2.EndTime,
t2.Success,
t2.Warning,
t2.Error
FROM [dbo].[jp_R_ITEM] t1 inner join(
select top(100) max([EXECUTION_DTTM]) EndTime, REQUEST_ID,
count([ITEM_ID]) as Transactions,
coalesce(count(case when [ITEM_STATUS_CD] = 0 then 1 end), 0) * 100 / count([ITEM_ID]) as Success,
coalesce(count(case when [ITEM_STATUS_CD] = 1 then 1 end), 0) * 100 / count([ITEM_ID]) as Warning,
coalesce(count(case when [ITEM_STATUS_CD] > 1 then 1 end), 0) * 100 / count([ITEM_ID]) as Error
from [dbo].[jp_R_ITEM] group by REQUEST_ID order by REQUEST_ID desc) t2 on t1.[REQUEST_ID] = t2.REQUEST_ID and t1.[EXECUTION_DTTM] = t2.EndTime
So from all your Transactions with RequestId 1, you want to make one element. This one element should have the RequestId, which in this case is 1, it should have the latest value of all ExecutionDttms or transactions with RequestId, and finally, from all those transaction you want a percentage of successes, warnings and errors
You want something similar for the Transactions with RequestId 2, and for the Transactions with RequestId 3, etc.
Whenever you see something like: "I want to group all items from a sequence into one object" you should immediately think of GroupBy.
This one object might be a very complex object, a List, a Dictionary, or an object of a class with a lot of properties
So let's first make groups of Transactions that have the same RequestId:
var groupsWithSameRequestId = Transactions
.GroupBy(transaction => transaction.RequestId);
Every group has a Key, which is the RequestId of all elements in the Group. Every group is (not has) the sequence of all Transaction that have this RequestId value.
You want to transform every group into one result element. Every result element
has a property RequestId and the number of transactions with this RequestId.
The RequestId is the Key of the group, the TransactionCount is of course the number of elements in the group
var result = groupsWithSameRequestId.Select(group => new
{
RequestId = group.Key,
TransactionCount = group.Count(),
...
};
Those were the easiest ones.
Endtime is the maximum value of all ExecutionDttm in your group.
var result = groupsWithSameRequestId.Select(group => new
{
RequestId = group.Key,
TransactionCount = group.Count(),
EndTime = group.Select(groupElement => groupElement.ExecutionDttm).Max(),
...
};
It might be that your data query translator does not allow to Max on Datetime. In that case: order descending and take the first:
EndTime = group.Select(groupElement => groupElement.ExecutionDttm)
.OrderByDescenting(date => date)
.First(),
First() is enough, FirstOrDefault() is not needed, we know there are no groups without any elements
We have save the most difficult / fun part for the last. You want the percentage of success / warning / errors, which is the number of elements with ItemStatus 0 / 1 / more.
Success = 100 * group
.Where(groupElement => groupElement.ItemStatus == 0).Count()
/group.Count(),
Warning = 100 * group
.Where(groupElement => groupElement.ItemStatus == 1).Count()
/group.Count(),
Error = 100 * group
.Where(groupElement => groupElement.ItemStatus > 1).Count()
/group.Count(),
It depends a bit on how smart your IQueryable / DbContext is. But at first glance it seems that Count() is called quite often. Introducing an extra Select will prevent this.
So combining this all into one LINQ statement:
var result = Transactions
.GroupBy(transaction => transaction.RequestId)
.Select(group => new
{
RequestId = group.Key
GroupCount = group.Count(),
SuccessCount = group
.Where(groupElement => groupElement.ItemStatus == 0).Count(),
WarningCount = group
.Where(groupElement => groupElement.ItemStatus == 1).Count(),
ErrorCount = group
.Where(groupElement => groupElement.ItemStatus > 1).Count(),
EndTime = group
.Select(groupElement => groupElement.ExecutionDttm)
.Max(),
})
.Select(item => new
{
RequestId = item.RequestId,
TransactionCount = item.GroupCount,
EndTime = item.EndTime,
SuccessCount = 100.0 * item.SuccesCount / item.GroupCount,
WarningCount = 100.0 * item.WarningCount / item.GroupCount,
ErrorCount = 100.0 * item.ErrorCount / item.GroupCount,
}
var query = (from t1 in lst
join t2 in (from b in lst
group b by b.REQUEST_ID into grp
select new
{
EndTime = (from g1 in grp select g1.EXECUTION_DTTM).Max(),
REQUEST_ID = grp.Key,
Transactions = grp.Count(),
Success = ((from g2 in grp select g2.ITEM_STATUS_CD).Count(x => x == 0)) * 100 / grp.Count(),
Warning = ((from g3 in grp select g3.ITEM_STATUS_CD).Count(x => x == 1)) * 100 / grp.Count(),
Error = ((from g4 in grp select g4.ITEM_STATUS_CD).Count(x => x > 1)) * 100 / grp.Count(),
}).OrderByDescending(x => x.REQUEST_ID).Take(100)
on new { RID = t1.REQUEST_ID, EXDT = t1.EXECUTION_DTTM } equals new { RID = t2.REQUEST_ID, EXDT = t2.EndTime }
select new
{
t1.REQUEST_ID,
t2.Transactions,
t2.EndTime,
t2.Success,
t2.Warning,
t2.Error
}).Distinct().ToList();

SQL ROW_NUMBER() in LINQ Query Syntax

I have this query which I wish to replace with Linq as query syntax:
select
ii.Id, ii.Name as Supplier,
qi.Price1, qi.Price2, qi.Price3,
case when qi.price1 is null then NULL else ROW_NUMBER() OVER(ORDER BY isNull(qi.price1,1000000) ASC) end AS Price1Order,
case when qi.price2 is null then NULL else ROW_NUMBER() OVER(ORDER BY isNull(qi.price2,1000000) ASC) end AS Price2Order,
case when qi.price3 is null then NULL else ROW_NUMBER() OVER(ORDER BY isNull(qi.price3,1000000) ASC) end AS Price3Order
From dbo.InquiryItems ii
left join dbo.quoteItems qi on ii.Id = qi.QuoteItem_InquiryItem
SQL-Query Result:
Id Supplier Price1 Price2 Price3 Price1Order Price2Order Price3Order
1655 Supplier 2 80.00 NULL 40.00 3 NULL 1
1656 Supplier 4 65.00 30.00 42.00 2 1 2
1662 Supplier 1 15.00 35.00 43.00 1 2 3
1670 Supplier 3 250.00 NULL NULL 4 NULL NULL
In C# I need this query as a IQueryable Object. I must filter the query for different parts (one or more) and then group it by Supplier (IdAccount) and SUM the prices. This prices I must rank.
return colQuoteItem = from vQuote in this.vQuoteItemOverviews
where vQuote.IdConstructionStageId == ConstructionStageId
group vQuote by new
{
vQuote.IdAccount
} into g
select new vQuoteItemOverviewSum
{
Id = g.Max(x => x.Id),
Price1Order = null, //Here I need the ROW_NUMBER like in the SQL-Syntax
Price2Order = null, //Here I need the ROW_NUMBER like in the SQL-Syntax
Price3Order = null, //Here I need the ROW_NUMBER like in the SQL-Syntax
price1 = g.Sum(x => x.price1),
price2 = g.Sum(x => x.price2),
price3 = g.Sum(x => x.price3),
}
;
Would this work?
var qi1 = (from qi in quoteItems orderby qi.price1 select new {qi.QuoteItem_InquiryItem}).AsEnumerable().Select((i, index) => new {i.QuoteItem_InquiryItem, Rank = index + 1});
var qi2 = (from qi in quoteItems orderby qi.price2 select new {qi.QuoteItem_InquiryItem}).AsEnumerable().Select((i, index) => new {i.QuoteItem_InquiryItem, Rank = index + 1});
var qi3 = (from qi in quoteItems orderby qi.price3 select new {qi.QuoteItem_InquiryItem}).AsEnumerable().Select((i, index) => new {i.QuoteItem_InquiryItem, Rank = index + 1});
return colQuoteItem = from vQuote in this.vQuoteItemOverviews.AsEnumerable()
where vQuote.IdConstructionStageId == ConstructionStageId
group vQuote by new
{
vQuote.IdAccount
} into g
select new vQuoteItemOverviewSum
{
Id = g.Max(x => x.Id),
Price1Order = qi1.FirstOrDefault(_ => _.IdConstructionStageId == x.Id)?.Rank, //Here i need the ROW_NUMBER like in the SQL-Syntax
Price2Order = qi2.FirstOrDefault(_ => _.IdConstructionStageId == x.Id)?.Rank, //Here i need the ROW_NUMBER like in the SQL-Syntax
Price3Order = qi3.FirstOrDefault(_ => _.IdConstructionStageId == x.Id)?.Rank, //Here i need the ROW_NUMBER like in the SQL-Syntax
price1 = g.Sum(x => x.price1),
price2 = g.Sum(x => x.price2),
price3 = g.Sum(x => x.price3),
}
;

C# Group with conditional

how can i grouping a data with conditional if bill < 10 ?
i have table:
meetingId | bill
------------------
a | 6
b | 7
c | 1
a | 5
a | 3
b | 4
g | 2
expected results :
a = 6+5+3 = 14 limit is 10 --> 10 and 4
b = 7+4 = 11 so limit is 10 --> 10 and 1
c and g not over the limit.
meetingId | bill
------------------
a | 10
a | 4
b | 10
b | 1
c | 1
g | 2
i tried in SQL why but i stuck with if condition
my SQL :
SELECT NO_ORDRE
,ORDRE.CODE_CLIENT As CodeCl
,[CODE_DEST]
,ORDRE.RS_NOM As OrdreRS
,ORDRE.ADRESSE As OrdreAdr
,ORDRE.CP As OrdreCP
,ORDRE.VILLE As OrdreVille
,ENLEV_CREMB
,ENLEV_DECL
,MODAL_MODE
,[PAYS]
,[INSEE]
,[SIRET]
,ORDRE.TEL As OrdreTel
,ORDRE.FAX As OrdreFax
,[EMAIL]
,[NBR_COLIS]
,[POID]
,[OBS]
,[DATE_CREE]
,[DATE_MODIF]
,[REF_EXPED]
,[AUTRE_REF]
,[AGENCE]
,[TRANSPORTEUR]
,NOM
,CAPITAL
,LIBELLE
,T_LOGO.IMG As FaImg
,T_LOGO.ADRESSE As FaAdr
,T_LOGO.CP As FaCp
,T_LOGO.VILLE As FaVille
,T_LOGO.TEL As FaTel
,T_LOGO.FAX As FaFax
,FAWEB_CLIENT.RS_NOM As CliRsNom
,FAWEB_CLIENT.ADRESSE As CliAdr
,FAWEB_CLIENT.CP As CliCp
,FAWEB_CLIENT.VILLE As CliVille
FROM [ORDRE]
LEFT JOIN T_LOGO ON ORDRE.TRANSPORTEUR = T_LOGO.NOID
LEFT JOIN FAWEB_CLIENT ON ORDRE.CODE_CLIENT = FAWEB_CLIENT.CODE_CLIENT
WHERE (STATUT_ORDRE = 2) AND (TRANSPORTEUR IN (SELECT ParsedString From dbo.ParseStringList(#Trans)))
and then i use in C#
List<Pers_Ordre> oListOrdre = new List<Pers_Ordre>();
while (readerOne.Read())
{
Pers_Ordre oPerOrdr = new Pers_Ordre();
Pers_Ordre test = (from t in oListOrdre where t.DestId == readerOne["CODE_DEST"].ToString() select t).FirstOrDefault();
oPerOrdr.OrdreId = Convert.ToInt32(readerOne["NO_ORDRE"]);
oPerOrdr.DestId = readerOne["CODE_DEST"].ToString();
if (test == null)
{
oListOrdre.Add(oPerOrdr);
}
else
{
int NbrColis = (from t in oListOrdre where t.DestId == readerOne["CODE_DEST"].ToString() select t.NbrColis).FirstOrDefault();
if (NbrColis < 5)
{
test.NbrColis += NbrColis;
}
}
}
it not work what i expected.
Thanks for your help!
(Not really an answer, but this doesn't fit in a comment.)
Here's a LINQ-to-Objects query that groups items by meetingId and creates new items such that there is one item with bill less than 10 and as many items as needed with bill equalling 10 to keep the sum:
Is this what you're looking for?
Code:
var list = new List<Tuple<char, int>>
{
Tuple.Create('a', 6),
Tuple.Create('b', 7),
Tuple.Create('c', 1),
Tuple.Create('a', 5),
Tuple.Create('a', 3),
Tuple.Create('b', 4),
Tuple.Create('g', 2),
};
var result = list
.GroupBy(x => x.Item1)
.Select(g => new
{
Key = g.Key,
Sum = g.Sum(x => x.Item2)
})
.Select(p => new
{
Key = p.Key,
Items = Enumerable.Repeat(10, p.Sum / 10)
.Concat(Enumerable.Repeat(p.Sum % 10, 1))
})
.SelectMany(p => p.Items.Select(i => Tuple.Create(p.Key, i)))
.ToList();
This SQL query will return the wanted results:
SELECT meetingId, SUM(bill) as bill_total
FROM table
GROUP BY meetingId
HAVING SUM(bill) < 10
You should not do this at the client side because it can get pretty intensive, a simple GROUP BY with a HAVING clause should give you the expected results:
Sample data:
The query you need:
SELECT
MeetingID,
SUM(bill) AS Total
FROM
Table_1
GROUP BY
MeetingID
HAVING
SUM(bill) < 10
The results of the query:
table.GroupBy(p => p.meetingId).Where(p => p.Sum(q => q.bill) < 10)
.Select(p => new
{
meetingId= p.Key,
bill= p.Sum(q => q.bill)
});

LINQ query - Multiple where with multiple sub queries based on int array

I have a sql query that performs the type of select I'm after:
select * from Products p where p.ProductId in (
select distinct ProductId from ProductFacets
where ProductId in (select ProductId from ProductFacets where FacetTypeId = 1)
and ProductId in (select ProductId from ProductFacets where FacetTypeId = 4)
)
There can be multiple FacetTypeIds passed into this query.
This query is constructed in a method based on a parameter argument of type int[].
public IEnumerable<Product> GetProductsByFacetTypes(string productTypeSysName, int[] facetTypeIds)
I'm trying to work out how to achieve this in LINQ. So far I've come up with something like this:
var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where p.FacetTypes.Any(x => x.FacetTypeId == 1)
where p.FacetTypes.Any(x => x.FacetTypeId == 4)
select p;
This returns the correct result set.
However I'm not sure how I can build this query using the int[] facetTypeIds parameter.
EDIT:
ProductFacets contains the following data:
ProductId, FacetTypeId
1, 1
1, 2
2, 1
2, 3
3, 4
3, 5
4, 1
4, 2
As an example, I'd like to be able to select only Products which have a FacetTypeId of 1 AND 2.
The result set should contain ProductIds 1 and 4
A local collection may be transmitted to the database by calling Contains:
from ft in facetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft;
The local collection is translated into sql parameters. Sql Server has a limit of ~2100 parameters, so beware.
Products that have any of the facets
from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where
(
from ft in p.FacetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft
).Any()
select p;
Products that have all facets.
int facetCount = facetTypeIds.Count();
from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where
(
from ft in p.FacetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft.FacetTypeId
).Distinct().Count() == facetCount
select p;
EDIT: Sorry I misread the question. I would suggest you use a PredicateBuilder and build up the Where clause dynamically if you need all of the types to be present. This would use extension methods directly.
var facetTypeIds = new [] { 1, 4, ... };
var predicate = PredicateBuilder.True<Product>();
foreach (int id in facetTypeIds)
{
int facetId = id; // avoid capturing loop variable
predicate = predicate.And( p => p.FacetTypes.Any( x => x.FacetTypeId == facetId );
}
var products = sc.Products
.Where( p => p.ProductType.SysName == productTypeSysName )
.Where( predicate );
Original (wrong, but left for context):
You want to use Contains. Note also you can use the logical and to replace multiple Where clauses with a single Where clause.
var facetTypeIds = new [] { 1, 4, ... };
var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
&& p.FacetTypes.Any( x => facetTypeIds.Contains( x.FacetTypeId ) )
select p;
This is based on tvanfosson's code. I have doubts about the performance of this approach though.
var facetTypeIds = new [] { 1, 4, ... };
var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
&& facetTypeIds.All(ft => p.FacetTypes.Any(x => x.FacetTypeId == ft))
select p;

Categories

Resources