score:1
you can use the following to display the generated sql for the linq statement.
reachdirectdatacontext rddc = new reachdirectdatacontext(connstring);
rddc.log = console.out;
return (from b in rddc.brokernos
from p in dc.promotions
where p.source == source && p.broker == b.brokerno1
select new promotion() {code=p.code,brokername=b.name}).tolist<promotion>();
you can try the following to separate out the queries.
var promotions = from p in dc.promotions
where p.source == source
select p;
var brokers = from o in promotions
join b in rddc.brokernos on o.broker equals b.brokerno1
select new promotion
{
code = o.code,
brokername = b.name
};
return brokers.tolist();
score:0
the double from looks suspicious to me. (this is not the equivalent to sql's join statement, if that is what you were aiming for.)
if you can combine the contexts:
try creating relationship between brokernos and promotions in edmx and using navigation property in query.
for example:
var result = dc.promotions.where(p => p.source == source).
select(p => new promotion() {
code = p.code,
brokername = p.broker.name, // use new navigation property here
});
if not (intersection will be done in memory, not on db!!!:
var result1 = dc.promotions.where(p => p.source == source).
select(p => new promotion() {
code = p.code,
brokerid = p.brokerid, // add id property for intermediate results
}).tolist();
var result2 = rddc.brokers.tolist();
var finalresult = result1.where(p => result2.contains(b => b.brokerid == p.brokerid)).select(p => new promotion{
code = p.code,
brokername = result2.single(b => b.brokerid == p.brokerid).name,
});
score:2
your linq statement looks fine. to aid in debugging, i find it helpful to assign the linq query to a local variable, then return the local variable. you can then set a breakpoint on the return statement, and when the debugger stops at the breakpoint, you can inspect the query local variable to see what's in it, interactively. you can use the locals window in vs, or the immediate window to surf around inside your app's variables and see what's going on.
in particular, double check that the inputs into your linq query are actually providing data. verify that rddc.brokernos is non-empty, and dc.promotions, etc. if these are empty, the result will be empty. track your bug "upstream".
minor point: you don't need to specify the type parameter on the .tolist() call in the select. the compiler will infer the type automagically.
Source: stackoverflow.com
Related Query
- linq query from two database
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Dynamic LINQ query to get Field value from Database
- linq query to join two tables and get the count from one table values from the other
- Update Linq query selecting from two tables?
- How to join two table from two different edmx using linq query
- Returning a LINQ database query from a Method
- Linq Query to Get Distinct Records from Two Tables
- creating Linq to sqlite dbml from DbLinq source code
- LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A
- LINQ code to combine two database tables?
- Linq query to get all values from between two dates
- Linq Query Filter from two lists where row differs
- Two LINQ statements to one awaitable LINQ database query (Optimize)
- string.Join Linq query to merge two strings from an array and output as single comma delimited string
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- image from database display in PicutreBox, using LINQ query
- Can't add a new record with an integer value into database by using linq from code C#
- Mapping from join - The type 'CarDTO' appears in two structurally incompatible initializations within a single LINQ to Entities query
- Adding to two different lists from one LINQ query
- combine two list and linq query to order by two different fields from different lists
- SQL Query or Linq query to get No of Events per day from database
- Create a LINQ query that fetches data from two Entities
- Linq query and Foreach on large number of records from SQL database
- Return Enum Name from Integer in LINQ Database Query
- Linq query to get data from Database using group by clause
- SQL query shows good database values, but LINQ to Entity framework brings a null value from nowhere
- LINQ - Merge two queries and exclude items from the first query
- How to apply between clause on two columns in Linq query while searching from datatable C#?
- How to get two sums from a linq query
More Query from same tag
- How to transform an enumeration of long to a single string with LINQ
- Include or exclude where clause in Linq to Sql c# function
- insert 1000 records using LINQ
- How do you get the properties, operators and values from an Expression<Func<T, bool>> predicate?
- Multiple columns in Linq
- how to order result by displaying each job type by order
- How to order an RSS Feed from a News Site by the Publication Date
- Using LINQ to build Hierarchical Navigation for Repeater
- How to make a right join using LINQ to SQL & C#?
- LINQ: converting a query expression to use lambdas
- EF Core how to use linq to match field stores as string but contains items separated by a delimiter
- Summing up double lists in a Linq GroupBy
- CRM 2011 objects LINQ query
- Wait for DomainContext.Load<t> from an entityquery with joins to complete (returning new type via 'select new')
- Turn SQL into Lambda/Linq
- Cannot implicitly convert type System.Collections.Generic.List
- How to Replace CSV String in LINQ Query , Wanted to Replace CSV with NewLine in LINQ
- Linq exception when join a list on a query result
- Select Using linq
- How to iterate a tree structure with logic on where to stop
- Dynamic LINQ: Comparing Nested Data With Parent Property
- Is there a linq-y way to union collection properties of a collection of objects?
- How Lambda Expression works
- How to filter a list using LINQ when the list has same properties?
- How to write nested query in Linq
- Flip a LINQ object
- EF code first make one to many relationship
- Write Complex Linq Join with Lambda Expression
- Linq Group By, Skip 1 where the skipped 1 was the most recent
- Can a LINQ extension method create a new KeyValuePair with a new() .Value when a Where clause isn't satisfied