score:1
you need to do like this:
var result = from at in db.assettagging
join r in db.returns on at.assetid equals r.assetid into a
from returns into a.defaultifempty()
join i in db.issues on at.assetid equals i.assetid into b
from issues into b.defaultifempty()
where issues.assetid != null || returns.requeststatus == "approved"
select new
{
assetid = at.assetid,
assetdescription = at.assetdescription,
status = returns != null ? returns.requeststatus : null
}.tolist();
score:0
here is the complete query
var result = (from assettagging in db.assettagging
join return0 in db.return on assettagging.assetid equals return0.assetid into returns
from return0 in returns.defaultifempty()
join issue in db.issue on assettagging.assetid equals issue.assetid into issues
from issue in issues.defaultifempty()
where issue.assetid == null || return0.requeststatus == "approved"
select new
{
assettagging.assetid,
assettagging.assetdescription,
return0.requeststatus
}).tolist();
score:1
try the following i am assuming that you still want the cases where r is null unless r is not null and request status = approved.
you have to check to verify r!=null before checking the request status and you will still need to include when r is null to get the complete result set. i haven't tested this, but this should put you in the right direction.
good luck.
var result = (from at in db.assettagging
join r in db.return.defaultifempty()
on at.assetid equals r.assetid
join i in db.issue.defaultifempty()
on at.assetid equals i.assetid
where
(r == null || (r!=null && r.requeststatus == "approved"))
|| i == null
select new {
at.assetid,
at.assetdescription,
issueid = (i!=null) ? i.issueid : null),
returnid = (r!=null) ? r.returnid: null),
returnstatus = (r!=null)
? r.returnstatus
: null}).tolist();
score:1
try like following:
from at in db.assettagging
join r in db.return on at.assetid equals r.assetid into res1
from atr in res1.defaultifempty()
join i in db.issues on i.assetid==at.assetid into res2
from obj in res2.defaultifempty()
select at
where i.assetid == null || r.requeststatus equals "approved"
just make two times left outer join and then do filter on where condition.
also have first look at this msdn article about left outer join using linq.
score:1
i know this isn't exactly what you've asked for, but it might be useful anyway.
if you have access to the database to execute sql queries, i would suggest creating a view. you can then drop the view into your dbml file the same way as you would with a table, and have much simpler linq expressions in your c# code.
create view [asset_issue_return_joined] as
select assettagging.assetid, assettagging.assetdescription, [return].requeststatus
from assettagging
left outer join [return] on assettagging.assetid = [return].assetid
left outer join issue on assettagging.assetid = issue.assetid
where (issue.assetid is null) or ([return].requeststatus = 'approved')
score:2
you need to remove .asenumerable()
, because you want your query to be translated to sql
. right now it would be using linq-to-objects
and if you are using a left join with linq-to-object
you need to check for null reference exceptions. rt
could be null, so rt.requeststatus
would throw an exception.
*i believe rt
should be r
in your example
you can't project to an existing entity, so you need to change your select to:
select new pococlass
{
model1=at
}
//new class definition
public pococlass
{
public assettagging model1 { get; set; }
}
Source: stackoverflow.com
Related Query
- Unable to convert SQL Query to LINQ Query for Left Outer Join
- how to convert a sql query into linq LEFT OUTER JOIN
- Convert simple Left Outer Join and group by SQL statement into Linq
- LINQ Left join on my than one field? I have the SQL working but can't convert it to a linq query
- convert sql to linq with left outer join
- How to convert left join sql query to linq to sql
- Convert SQL to Linq for left join with NULL in right
- Convert SQL Request to linq query c# (left outer join and inner join)
- convert sql query to linq with complex left join
- How to use left outer join in LINQ for SQL query?
- Need to convert Left join SQL to linq query - help appreciated
- Converting Oracle SQL query with left outer join to Linq
- SQL query to LINQ left outer join
- Convert SQL query (with outer join and datediff) to LINQ
- LINQ to SQL - Left Outer Join with multiple join conditions
- LINQ to SQL Left Outer Join
- Convert SQL to Linq left join with null
- LINQ to SQL multiple tables left outer join
- Extension method for IQueryable left outer join using LINQ
- How do I most elegantly express left join with aggregate SQL as LINQ query
- LINQ left outer join query error: OuterApply did not have the appropriate keys
- Left outer join using LINQ -- understanding the code
- EF Linq to Entities calling ToList() on entity set generates SQL command containing multiple left outer join
- Determine the source DataContext for a Linq to Sql query
- How to add left outer join to grouped and summed LINQ query
- LINQ to Sql Left Outer Join with Group By and Having Clause
- Left Outer Join in Linq to Entities / SQL
- Left outer join using LINQ Query Syntax EF Core C#
- Linq to SQL Left Outer Join Not
- C# - from SQL to Linq - Left Outer Join/Inner Join
More Query from same tag
- How can I let users specify which entity properties are used in a Linq-to-Objects GroupBy query expression at runtime?
- How to get unique entries of products in a List<Order> with LINQ
- Typecast a value and get a property Linq
- How to Split a string using the delimiter with quotes and white spaces?
- LINQ part of string is contained in list members
- Search all items with LINQ C# WP8.1
- Write Dynamic LINQ queries for sorting and projecting with EF Core
- Populating classes with many to many linq data
- Query to detect duplicate rows
- Performance wise what is the best way to compare 2 DataTables with approximate 1000 records?
- Efficient join query in LINQ
- Get xml attribute value from an associated attribute value
- Entity framework recursively populate children of DTO
- C# Using GetProperty for LINQ OrderBy using FileInfo
- SQL code to join to, and sum data in, a table referenced by comma delimited keys
- Reuse select new object in linq statement
- Linq Filtering items with ForEach crashes for large data set
- How to check if Linq Expression value is null
- How can I provide default expression in the { } of a lambda expression, while still allowing it to be added to?
- GridView DynamicField child property generates "table does not have column" error
- linq to entities, a where in where clause? (inner where)
- How to do multiple split values from a list of string to an object, blows if there is no string to split
- linq get array elements query
- Using LinqToSql without load base in memory
- MVC LINQ Converting from datetime to week
- Dynamic query to immediate execute?
- linq - find item in list within multiple lists
- Is there a Linq equivalent for creating collection from subset of an object used in a previous collection
- PartialView passing Customized List using LINQ
- Read XML file using Xml.Xpath and Xml.Linq