score:5
if your test
entity has a usertests
collection you can use this query:
string userid = "1";
var result = context.tests
.selectmany(t => t.usertests
.where(ut => ut.userid == userid)
.defaultifempty()
.select(ut => new
{
examid = t.examid,
testid = t.testid,
title = t.title,
usertestid = (int?)ut.usertestid,
userid = ut.userid,
result = ut.result
}))
.orderby(x => x.examid)
.thenby(x => x.testid)
.thenby(x => x.usertestid)
.tolist();
using defaultifempty()
here ensures a left outer join
so that you always have at least one usertest
entity (which is possibly null
) for a given test
. casting the non-nullable properties of the usertest
- like usertestid
- to a nullable type (int?
for example) is important here, otherwise you can get an exception that a null
value returned from the database can't be stored in a non-nullable .net type.
if you don't have and don't want a usertests
collection in you test
entity you can use a groupjoin
as alternative which would basically left outer join the two tables by the testid
:
string userid = "1";
var result = context.tests
.groupjoin(context.usertests.where(ut => ut.userid == userid),
t => t.testid,
ut => ut.testid,
(t, utcollection) => new
{
test = t,
usertests = utcollection
})
.selectmany(x => x.usertests
.defaultifempty()
.select(ut => new
{
examid = x.test.examid,
testid = x.test.testid,
title = x.test.title,
usertestid = (int?)ut.usertestid,
userid = ut.userid,
result = ut.result
}))
.orderby(x => x.examid)
.thenby(x => x.testid)
.thenby(x => x.usertestid)
.tolist();
score:0
check this line here
you might get some idea for how to do this.
score:0
here is a link to a discussion that shows how to call stored procedures with a parameter: how to use dbcontext.database.sqlquery<telement>(sql, params) with stored procedure? ef code first ctp5
here is one way to code the stored procedure:
create procedure dbo.sample1 (
@oneid nvarchar(128) = n'xx') as
begin
set nocount on;
select @oneid as userid,
r.testid,
r.result
from (
select t.userid, e.testid, t.result
from dbo.usertest as e
left outer join dbo.usertest as t on e.testid = t.testid and t.userid = @oneid
where e.userid = 0) as r
order by r.testid
end
go
score:0
try this:
var tests = context.tests.include( "exam" )
.select( t => new
{
test = t,
usertests = t.usertests.where( ut => ut.userid == studentid )
} )
.tolist();
score:1
var tests = (from t in context.tests
// where !t.userttests.any() //if no user took the test
// || t.usertests.any(ut=>ut.student.studentid == stid)
select new {test = t, exam = t.exam,
usertests = t.usertests.where(ut=>ut.student.studentid == stid))
.tolist();
on 2nd thought, may be this will be better. this will give you exam, test and usertests if there is any matching ones or null usertests
Source: stackoverflow.com
Related Query
- How can I code an outer join using LINQ and EF6?
- How can I query my join table (many-to-many) using Linq and EF6 (get a users roles)
- How can I implement a LEFT OUTER JOIN in LINQ using lambda syntax on Entity Framework Core 2.0?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How can I join a list of employees and genders using linq
- How can I create a join like query using LINQ and check for a propery in a child object?
- How do you perform a left outer join using linq extension methods
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- Left outer join using LINQ -- understanding the code
- How can I write the following code more elegantly using LINQ query syntax?
- Left outer join null using VB.NET and LINQ
- How to add left outer join to grouped and summed LINQ query
- How can I use Linq to join between objects and entities?
- Best way to join 3 lists and count how many times an ID is in the list using linq
- How can I get the top three players and their high scores from a collection using linq and lambdas
- How to Join in LINQ using Lambda expressions and get the result in DTO?
- how to implement join using LINQ and EntityFramework
- How do I do a table join using LINQ and entity framework 6?
- How can I filter a list using generics and Linq extension methods?
- How can I LINQ outer join two collections?
- Linq to SQL With Left Outer Join and Group By With Sum - How To
- Using LINQ to XML, how can I join two sets of data based on ordinal position?
- How to join two tables using Linq and count the rows of one column matched records
- How can I join data with Linq to Entities and WCF Data Services?
- How to JOIN two data tables using LINQ and Where condition?
- How Can i retrieve date from a hashset that contains date and name using linq or contains?
- How to join tables using LINQ and navigation properties
- How can I create SQL that does a Left Outer Join and also a count from another table?
- How can i join two table in Linq and display the join to Data GridView?
More Query from same tag
- Linq one to many relations as comma separated values
- Sum the difference of two columns in Entity Framework
- Convert LINQ written in VB.NET to C#
- Failed to get records when matching c# DateTime value against Date value in sql
- LINQ-to-SQL and Extension method in subQuery
- Linq - Joining through a dictionary where the KeyValuePair.Value is a collection itself
- How to pass table name dynamically in linq to sql
- LINQ select new with collection
- Entity Framework groupby day to day in month
- LINQ delete error (C#)
- filtering results with Entity Framework
- How to extend the ADO.NET entity designer?
- How to check for null attributes in LinqToXML expressions?
- How to put XElement Document records inside a list of objects
- Combine two sequences with different types
- Lambda Expressions and searching
- linq select statement vs direct access
- Converting a SQL Query to a Predicate Expression using Fluent Synatx
- Linq - What is the quickest way to find out deferred execution or not?
- Obtain all middle elements IEnumerable LINQ
- LINQ - GroupBy multiple columns and merge the result
- EF Core - Relationship between 2 entities for non-PK columns
- Full Text Search in Linq
- select two properties in an array of obj
- Linq get last ID inserted
- Using XDocument to build XHTML with nested tags
- Error while insert/update records in asp.net core
- How to return all ranges that are not included in any other range C# list
- The column cannot contain null values. SqlCE exception
- How to access the query variable