score:1
Table Item: ItemId
Table Rating: UserId, ItemId1, ItemId2, WinnerId
If you require that ItemId1 < ItemId2 in the Rating table, you only have to check the Rating table once.
var pair = db.Items.Join(db.Items,
i1 => i1.ItemId,
i2 => i2.ItemId,
(i1, i2) => new {i1, i2}
) //produce all pairs
.Where(x => x.i1.ItemId < x.i2.ItemId) //filter diagonal to unique pairs
.Where(x =>
!db.Ratings
.Where(r => r.UserId == userId
&& r.ItemId1 == x.i1.ItemId
&& r.ItemId2 == x.i2.ItemId)
.Any() //not any ratings for this user and pair
)
.OrderBy(x => db.GetNewId()) //in-database random ordering
.First(); // just give me the first one
return new List<Item>() {pair.i1, pair.i2 };
Here's a blog about getting "random" translated into the database.
score:0
Assuming that the list of available items is in the database, I would handle this problem entirely in the database. You are hitting the database already, no matter what, so why not get it done there?
score:0
What about putting all the objects in a queue or a stack, and then pop 2 and 2 off until they are empty?
score:1
One solution is this:
SELECT TOP 1 i.id item1, i2.id item2 from item i, item i2
WHERE i.id <> i2.id
AND (SELECT COUNT(*) FROM Rating WHERE userId=@userId AND FK_ItemBetter=i.id AND FK_ItemWorse=i2.id) = 0
AND (SELECT COUNT(*) FROM Rating WHERE userId=@userId AND FK_ItemBetter=i2.id AND FK_ItemWorse=i.id) = 0
ORDER BY NEWID()
I wasn't aware of the cross join method of just listing multiple FROM tables before.
Source: stackoverflow.com
Related Articles
- Find a combination of two elements that have not been viewed together (LINQ, SQL or C#)
- Find list of xml elements that have a list of names using linq (and remove them)
- Given UIElementCollection, find all elements that have StyleA, and change them to StyleB in WPF
- How do I use Linq to find the elements of a list that are not present in another list?
- How do I use LINQ to find 5 elements in a row that match one predicate, but where the sixth element doesn't?
- Need a LINQ code example to link two tables that have no foreign key
- Linq to XML, only take elements that have a certain child element
- Find sub elements that differ in two collections in Linq
- LINQ Group elements that are equal together
- Using Linq to find all records in table that have a specific value in a related table using method syntax
- Using Linq to XML, how can I select the elements that have exactly x number of parent elements?
- How to find out if one element in array has false value for property and if all elements have true value using Linq
- code that i have to do it through LINQ
- Linq find all elements from nested list which have id
- Using Linq to find only records that have all matching values in list of flags?
- I have some code that I would like to turn to linq
- LINQ to SQL to find match in results that have multiple pipe separated values
- What Linq function can i use to get all the combination sequences of two Enumerables that choose between the two elements of every index?
- Linq to XML - get elements that have certain child element
- LINQ - Find all items in one list that aren't in another list
- 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
- How to query an XDocument with LINQ when elements have a colon in their name?
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- LINQ to find the closest number that is greater / less than an input
- Is there a good source that gives an overview of linq optimizations?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Find elements existing in both collections using LINQ
- Using LINQ to find all keys from one collection that are not in another?
- Multiple left joins and combining equals and not equals in Linq
- LINQ Conditional Where clause including multiple conditions
- Filtering a list by a property matching a property in another list
- Create and insert from Temporary table using Linq to SQL in DBML
- Convert a datatable to IList c#
- Sort on unknown object.property WPF
- Merging 2 rows from 2 different lists with same UserId?
- timezone in Where clause of linq query
- How to get the Distinct item using linq
- linq join return type
- cal-heatmap data from linq query
- Error 2 '' does not contain a constructor that takes 1 arguments, (Topic: Typed Context in Linq to SQL )
- Call is failing with null parameter
- minimum value in dictionary using linq
- LINQ - How to join multiple tables and order by dynamically?
- Linq .Count() does not exist in VB.NET but exists in C#
- Filter a IEnumerable<string> for unwanted strings
- Query for string containing one or more of many strings
- CRM 2013 Retrieve all teams for a given user using LINQ
- retrieve last value from table in linq Extention method and bind with DataGridView