score:2
Accepted answer
join
in LINQ assumes an inner join (no nulls). Try pulling the null stuff out into separate where
clauses. I think something along these lines should work for what you're describing.
from lt in cxt.CmsPageRow
join page in cxt.CmsPage on lt.CmsPageID == page.CmsPageID
where cmsSiteID == null ||
(cmsSiteID != null && (page.CmsSiteID == null || page.CmsSiteId == cmsSiteID.Value))
select ...
Update
I didn't realize that performance was an issue for you. In that case, I'd suggest creating a different query structure based on values that are known at run-time and don't depend on individual rows:
var rows =
from lt in cxt.CmsPageRow
join page in cxt.CmsPage on lt.CmsPageID == page.CmsPageID
select new {lt, page};
if (cmsSiteID != null)
{
rows = rows.Where(r => r.page.CmsSiteID == null ||
r.page.CmsSiteId == cmsSiteID.Value));
}
var data = rows.Select(...);
Also, if your data context is set up right, you should be able to use navigation properties to simplify your code somewhat.
var rows = ctx.CmsPageRow;
if (cmsSiteID != null)
{
rows = rows.Where(r => r.CmsPage.Any(p => p.CmsSiteID == null ||
p.CmsSiteId == cmsSiteID.Value));
}
var data = rows.Select(...);
Source: stackoverflow.com
Related Articles
- LINQ join on columns AND nullable variable
- Select All columns for all tables in join + linq join
- linq to sql join on multiple columns using lambda
- Select all columns after JOIN in LINQ
- LINQ to SQL: Left join on multiple columns
- LINQ to Entities Join on Nullable Field where Null Implies "Match All"
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- how to get all columns from a linq result with a join
- LINQ Source Code Available
- LINQ Join On Multiple Columns With Different Data Types
- LINQ Join on a Nullable key
- LINQ to Entities, join two tables, then group and take sums of columns from both tables
- Linq left join on nullable field
- LINQ to SQL join 3 tables and select multiple columns and also using Sum
- How can I code an outer join using LINQ and EF6?
- DataTable Linq join many columns
- Linq 2 Entities : Performing a join on two columns with different types
- C# Linq Join 2 tables on multiple columns and GROUP BY for count
- Select multiple columns without join in LINQ
- LINQ Join query (with nullable ref between table)
- Problem with simple join by multiple columns in two tables using linq
- Finding common columns from two datatable and using those for Join condition in LINQ
- creating Linq to sqlite dbml from DbLinq source code
- LINQ Left join on nullable column giving null reference exception
- Generic Extension method for LINQ JOIN using 2 columns
- VB.net Linq join on two columns
- Join three list using multiple columns c# linq lambda
- Nullable Int in LINQ with outer join
- Join columns in LINQ and run contains operator
- Linq Efficiently find if collection contains duplicates using IQueryable.GroupBy
- NotSupportedException using ToList() in LINQ and EntityFramework
- Finding property differences in two lists
- LINQ Outer Join Has Duplicates
- Handling special character input for data selection
- Explain in small words why IQueryable<T> is needed
- LINQ Join to find items NOT IN a list
- Is there a way of splitting a C# generic dictionary into multiple dictionaries?
- How to get treeview parent and child nodes in dictionary
- Convert array of employees to Dictionary
- LINQ: Error converting HashSet<Dictionary<String, String>> to Dictionary<String, HashSet<Dictionary<String, String>>>
- Using date comparison in LINQ when querying SharePoint OData service
- NullReferenceException when trying to implement LEFT JOIN with Linq Lambda expression
- Reading related data is throwing a LINQ error in my application
- DataTable to List in C#
- Will this Post/Tag DB schema cause problems later?
- Unioning two LINQ queries
- How to get a count of the items returned by a LINQ query
- Finding duplicate texts in IEnumerable<TextBox> collection
- How to add some number to XML element's value (it's concatenating instead of adding)?