ADO.NET :: Left Join And Grouping In LINQ?
Aug 11, 2010I have the below query which has left join and count in SQL. Please help me convert this SQL query to LINQ (using Entity frameowrk).
[Code]....
I have the below query which has left join and count in SQL. Please help me convert this SQL query to LINQ (using Entity frameowrk).
[Code]....
I'm trying to build a linq2Entity/gridview function for a forum, that draws data from multible tables.The result should be: Get the latest 10 threads that user(xx) has replyed in, that is not disabled or has a subject that is not allowed.Include information on the last post, thread owner user, and last post user, in each thread.
ere is my code... is returns the correct threads and some that should not be there.
[Code]....
I have the following LINQ query that does a left join:
var results = from v in ctx.dat_Visitor
join sp in ctx.vwSP on v.lu_Mentor.ID equals sp.ID into sp2
select new {
[code]...
My problem is that this LINQ query times out on me. I even upped the default timeout from 15 seconds to 120 seconds and it still times out. dat_Visitor has no more than 10 records in it, but vwSP (which is a view) has somewhere around 100,000 records. I know for sure that the timeout has to do with when I attempt to actually return sp2 in the results. If I change my LINQ query to not return sp2 in the results, then it is quick. Without sp2 the query looks like such:
var results = from v in ctx.dat_Visitor
join sp in ctx.vwSP on v.lu_Mentor.ID equals sp.ID into sp2
select new {
Visitor = v.Name,
Tot = sp2.Count()
};
Lastly, I'm using the 3.5 framework, but I am having serious thoughts about upgrading the website to 4.0, since I could then use DefaultIfEmpty().
How to use Left and Right Outer Join in C# using Linq with Eg.
View 2 RepliesI'm having trouble doing a left outer join with multiple generic lists tables. I've been researching for hours and there isn't a whole lot out there on this subject and VB (at least what I found).
I have the following Linq query:
Dim DataForGridview = (From s In S1 _
Join c In C1 On c.ID Equals s.ID _
Join v In V1 On v.ID Equals s.ID _
Select New With { _
.dataFieldID = s.ID, _
.dataField1 = s.dataField1, _
.dataField2 = c.dataField2, _
.dataField3 = c.dataField3, _
.dataField4 = c.dataField4, _
.dataField5 = v.dataField5 _
}).ToList()
S1 has a count of 30. V1 has a count of 20. C1 has a count of 10. This query joins the three but ends up with a count of only 10 based on the datakey ID and, of course, only shows those 10 in the gridview.
I would like to show all 30 from S1 in the gridview with blank fields wherever V1 and C1 do not have data. From what I've read I need to do a left outer join. However, all examples I've found are with two datasets.
I've tried multiple times expanding on what I've found by changing it to a group join and also adding the .DefaultIfEmpty() property in the query, but nothing is working.
How can I change the above query to a left outer join with these multiple tables?
I have some text files I need to parse in order to display my data, and what I have now are two text files with lots of redundant data. Instead of this I would like to replace the redundant data with a number referencing the text in another text file.
View 1 Replieshow to do this query in EF1:
[Code]....
I have 2 different datatables. 2 datatables from 2 different servers.
My first datatable is this:
SELECT DISTINCT a.name, a.create_date, a.Modify_Date, a.Object_Id
FROM dbname.sys.objects a
and my 2nd datatable is the same, but from a different server connection string
SELECT DISTINCT b.name, b.create_date, b.Modify_Date, b.Object_Id
FROM dbName.sys.objects b
Basically, I want to compare which procedures are on one server and not the other (dev and prod)
So after I have my 2 datatables i want to create a data relationship on the name and then do a left join to see whats in a and not b.
I have 9 tables i need data out of.. 8 of those tables will always just return 1 row of fields that i need. the 9th table can contain many records.. So what i need is a few fields out of each of the 8 tables and then ALL records from the 9th table.. so i know that because there will be multiple records in the 9th table returned, that the fields i pull from the other 8 tables will contain duplicate information and thats fine since i will be displaying the individual records from table 9 on their own page..So everything could be for the same vendor, except for the date in the 9th table that contains specific dates and details about each record returned.When i review the execution plan im getting an yellow exclamation icon on a nested inner join and has this message as the warning: No Join Predicate
Here is what i have, but is returning incorrectly, way to may records, seems to be stuck in a loop.. then below that, i have one that returns the correct number of records, but everything except one column is duplicate, all the TR columns should be unique in the values stored in them.. the rest should all be duplicates, just because its the same vendor, so all the other information will be generic information about that record.
[Code]....
This FROM and WHERE return the correct number of records, but the fields are all duplicated except the last column with is transaction_date, seems that the 3 TR fields are being populated with the first records data or something.. not sure.
[Code]....
i want to delete data from two table using left outer join..
here is my query:
delete from projects p LEFT OUTER JOIN emp_project empp on p.serial_no=empp.projectid where p.serial_no=7
but i am getting error:
Incorrect syntax near 'p'.
I have a gridview showing values from two tables accommodation and pic (left join on accommodation). If there is no pic uploaded I want to display the row without the pic. So in the gridview I first check to see if the picid is null, if it is i don't render any 'a href' code , if it's isn't i do.
The problem that i'm having is that instead of the image showing up in the gridview itemtemplate i get
','_blank','toolbar=no,menubar=no'))" >
Here is my .aspx code
SelectCommand="SELECT pic.picid, aspnet_Users.Username, accommodation.location, accommodation.type,accommodation.description, convert(varchar,accommodation.createdate,101) as createdate
I need to make a category filter. For this purpose I have two tables "Ingredient" and "Category"
My Category Table looks like this:
[Code]....
And my ingredient table:
[Code]....
I want to join the two tables so when the KategoriID match it should then return all from that category.
[Code]....
[Code]....
I was trying to find out how I can join more than 2 tables using the LINQ-to-SQL syntax. For instance, joining 2 tables in SQL:
SELECT * FROM Table1 AS T1 INNER JOIN Table2 AS T2 ON T1.Key=T2.Column1WHERE T2.Key='17';
can be expressed as:
var Result = from T1 in DbContext.Table1 join T2 in DbContext.Table2 on T1.Key equals T2.ForeignKey where T2.Key=17 select new { T1, T2 };
But how would I join 3 or more tables using LINQ? For example:
SELECT * FROM (Table1 AS T1 INNER JOIN Table2 AS T2 ON T1.Key=T2.Column1) INNER JOIN Table3 AS T3 ON T3.Key=T2.Column2 WHERE T2.Key='37';
I've been searching and experimenting and I cannot seem to find any informraiton on this. One example I found involves putting the result for the frist join into a temp object, and then performing the second join. I'm not sure performance-wise if that's the same as doing a 3-table join directly using a single SQL statement.
i use Default membership provider for my membership
i add a profile table for some more information from user and it related with UserId with aspnet_Users
to get data from my database i use Entity Data Model
now i want to write a Linq to get data from all 3 tables
I have T_articles to save the articles.
each article has authorCode , and categoryCode.
each article can be modified by the author several times.
and I save every modification in the T_articles in a difference row.
now I want to dislpay for each autor:
author name, article name , article category's name, last modified date
In SQL I should to it like that:
[Code]....
I try to write it in Linq so:
[Code]....
so I got error:
[Code]....
and again I got an error:
[Code]....
Here's my DB structure:
I have three tables.
User, Details, Optional
User has fields: ID, FirstName, LastName
Details has fields: ID, MyDetails1, MyDetails2, User_ID
Optional has fields: ID, MyOptions1, MyOptions2, User_ID
There is a 1 to many relationship with User & Detail (User ID & Detail User_ID)
There is a 1 to many relationship with User & Optional (User ID & Optional User_ID)
With that said, one user can have many details and/or many optionals
Now I'm trying to build a linq query that will say:
"where user id = x, select FirstName, LastName, all MyDetails1 from every record where User_ID == x, all MyOptions1 from every record where User_ID == x"
After the merge I should have one string that lists the distict user & all of their many fields:
FirstName + LastName + (MyDetails1 + MyDetails1 + MyDetails1..etc) + (MyOptions1 + MyOptions1)
Anyone try this before?
I tried this with JOIN but this doesn't do the trick for what I'm looking for.
So if I had 3 MyDetails records associated with ID == 2, I would get three results like:
John Doe MyDetails_X
John Doe MyDetails_Y
John Doe MyDetails_Z
I am trying to put a Right Outer Join on Linq statements. how to put Right Outer Join on Linq.
View 1 RepliesI am new to LINQ. I have a GridView which I am populating using LINQ. My LINQ statement is taking query string from previous page. The query string is in string format. Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
string getEntity = Request.QueryString["EntityID"];
[code]...
The main problem is that I recieve the following message:
"base {System.SystemException} = {"Unable to create a constant value of type 'BokButik1.Models.Book-Author'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."}"
based on this LinQ code:
[Code]....
How shall I solve this problem to get a class instance that contain with property title and Book-AuthorID?I also have tried making some dummy by using "allbooks" relation with Code Samples from the address
http://www.hookedonlinq.com/JoinOperator.ashx. Unfortunately, still same problem.
I also have taken account to Int32 due to entity framework http://msdn.microsoft.com/en-us/library/bb896317.aspx. Unfortunatley, still same problem.
Using database with 3 tables and one of them is a many to many relationship. This database is used in relation with entity framework
Book-Author
Book-Author (int)
BookID (int)[code]....
private void GetResults()
Use an Expression on a Linq Outer Join
I ned to run a query with two joins, the second join does not work please help with the correct syntax.
[Code]....
I have this code:
public ActionResult Index()
{
MembershipUser currentUser = Membership.GetUser();
[code]...
I have this group-join syntax but I couldn't get the distinct value from the relational tables below.
Table_1
key_1 t1_value
1 Food
2 Sports
3 Leisure
4 Trip
5 Zoo
Table_2
key_2 key_1 t2_boolean
15 1 True
16 1 True
17 1 True
18 2 True
19 2 True
20 2 False
desire result:
[code]...
I am working on a website similar to nerddinner. Is it possible to perform similar join
using Linq to entities what is done with linq to sql in nerddinner.I am posting the codes below.
public IQueryable<Dinner> FindByLocation(float latitude, float longitude) {
var dinners = from dinner in FindUpcomingDinners()
join i in db.NearestDinners(latitude, longitude)
[code]...
I am having trouble with duplicate records. I tested .distinct() in several areas and nothing helps.
I got the search criteria from the PreviousPage. I then used this criteria to search QueryA.
I did a search using QueryA. I took the results, and put the IDs into an array.
I then did a search using QueryB, that matched the IDs in the array. QueryB is used for the ListView and to page records.
Using test records. QueryA results = 2 records. The array.length = 2. QueryB results = 3. ?
What am I doing wrong ?
Some of my Code:
myResults = (from r in QueryA
select r.ID).Distinct().ToArray();
totalrecords = myResults.Length; // = 2
using (ToHealthEN n = new ToHealthEN())
{
[Code]...