score:-1
public static int GetAdId(string KeywordId)
{
var dbContext = new MyDbContext();
var result = (from k in in dbContext.KeywordAdCategories
where k.Keyword_Id == KeywordId
select k.Ad_Id).SingleOrDefault();
return result;
}
Run all your keyword ids through this method and you will get the corresponding ad id. I recommend that you read up on LINQ. It is a fantastic tool once you get to know it, and it's not that hard to learn, although it might seem a bit scary at first. Pluralsight.com have a lot of great videos that are easy to grasp and learn from :)
score:0
List<string> keywordnames = new List<string>();
// populate keywordsnames list
IQueryable<int> AdIds = from k in KeywordAdCategories
where
(from kw in Keywords where keywordnames.Contains(kw.name) select kw.id)
.Contains(k.keyword_id)
select k.Ad_Id;
or
List<string> keywordnames = new List<string>();
// populate keywordsnames list
List<int> keywordnameids = (from kw in ctx.Keywords
where keywordnames.Contains(kw.name)
select kw.id).ToList();
List<int> AdIds = (from k in ctx.KeywordAdCategories
where keywordnameids.Contains(k.Keyword_Id)
select k.Ad_Id).ToList();
score:1
This was quite a bit trickier that I thought when first looking at it, so I am probably over thinking it. But here is a solution that I believe will work for you:
var keywordList = new List<string>();
keywordList.Add("ALFA");
keywordList.Add("145");
var results = KeywordAdCategories.Select (kac => kac.Ad_Id).Distinct()
.Select (a =>
new
{
AdId=a,
Keywords=KeywordAdCategories.Where(kac => kac.Ad_Id == a).Select(kac => kac.Keyword_Id)
})
.Where(ac => ac.Keywords.Intersect(Keywords.Where(kw => keywordList.Contains(kw.Name)).Select (kw => kw.Id)).Count() == keywordList.Count())
.Select (ac => ac.AdId);
Now to explain what I did here. First I wanted to start off with all the possible Ad id's so we have this line:
KeywordAdCategories.Select (kac => kac.Ad_Id).Distinct()
Of course this could probably be more easily retrieved from a distinct select from your ad table, but I am trying to only work with what you defined.
Next I select each ad id with its related collection of keywords into a list of anonymous typed objects:
.Select (a =>
new
{
AdId=a,
Keywords=KeywordAdCategories.Where(kac => kac.Ad_Id == a).Select(kac => kac.Keyword_Id)
})
Next I filter the results to only the objects that contain a collection of Keywords that intersects with our keyword list and has the same number of resulting elements as our keyword list.
.Where(ac => ac.Keywords.Intersect(Keywords.Where(kw => keywordList.Contains(kw.Name)).Select (kw => kw.Id)).Count() == keywordList.Count())
and finally I select out just the ad id's that result
.Select (ac => ac.AdId);
here is the emmited sql if you are intersted
-- Region Parameters
DECLARE @p0 NVarChar(1000) = 'ALFA'
DECLARE @p1 NVarChar(1000) = '145'
DECLARE @p2 Int = 2
-- EndRegion
SELECT [t1].[Ad_Id]
FROM (
SELECT DISTINCT [t0].[Ad_Id]
FROM [KeywordAdCategories] AS [t0]
) AS [t1]
WHERE ((
SELECT COUNT(*)
FROM (
SELECT DISTINCT [t2].[Keyword_Id]
FROM [KeywordAdCategories] AS [t2]
WHERE (EXISTS(
SELECT NULL AS [EMPTY]
FROM [Keywords] AS [t3]
WHERE ([t2].[Keyword_Id] = [t3].[Id]) AND ([t3].[Name] IN (@p0, @p1))
)) AND ([t2].[Ad_Id] = [t1].[Ad_Id])
) AS [t4]
)) = @p2
I am sure that there are much more elegant ways to accomplish what you are wanting to do, but I wanted to see if I could get it all to happen in a single linq query. Hope this helps.
Source: stackoverflow.com
Related Articles
- SQL to LINQ to get all records that match all values
- Linq query to select all records that have sub records that match all values in an array of values?
- Linq select records that match a list of IDs
- LINQ return records where string[] values match Comma Delimited String Field
- LINQ query (or lambda expression) to return records that match a list
- Using Linq to Entities to Return Records that Match a List of Possible Strings
- Get values from list that match BOTH attributes using c# LINQ
- Using Linq to find only records that have all matching values in list of flags?
- Using LINQ how do you filter a list of strings for values that match the pattern "q1, q2" etc.?
- C# Lambda Method syntax to obtain attribute values that match pattern in LINQ to XML
- LINQ Compare two arrays and return the position and values that do not match
- Trouble with linq left join only including values that are a match
- Need to get all records from one table, that match values I return from another table
- Using LINQ to return a list of entities that match all values in a list passed in as a parameter
- Return list of rows that match a list of values using LINQ
- LINQ to SQL to find match in results that have multiple pipe separated values
- How are people unit testing code that uses Linq to SQL
- How to count the number of elements that match a condition with LINQ
- Using LINQ to Objects to find items in one collection that do not match another
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- Is there a good source that gives an overview of linq optimizations?
- Match and update values using linq from two lists?
- How can I use linq to return integers in one array that do not match up with an integer property of another array?
- LINQ Source Code Available
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- Linq to return records that don't have a follow up record
- linq - how do you do a query for items in one query source that are not in another one?
- How do I use LINQ to find 5 elements in a row that match one predicate, but where the sixth element doesn't?
- Using datatable in place of list in linq
- How to sort & "crop" a dictionary list in C#
- How to write GroupJoin Lambda Expression in Linq query?
- (How) can I create a selector function with a type parameter as a variable?
- selecting multiple columns and looping through the selected rows
- How to calculate an ETA for a LINQ pipeline to complete?
- Get List of Styles
- Why does my code not find directories
- Not equal for linq operator in vb.net
- Efficient way to be updating child records when updating the Master table using Linq
- How to handle Creation & Modified Date in .NET MVC Entity Framework Migration?
- Select multiple rows & colums in datatable where value occurs more than once
- Linq executing query generates not supported exception
- ToListAsync() fails on ASP NET CORE but ToList() works - Error "Sequence contains no elements."
- One way syncing of 4 columns in SQL Server
- How to make a dynamic LINQ query?
- How to use LINQ to combine two or more collections into one collection
- I'm trying to populate a multidimensional array from a datatable with no success
- How to check in linq if the list is null?
- Creating a generic method in IEnumerable