Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am new to Linq and I want to convert the below LINQ query to SQL query,

C#
from m in (db.ForumMessages.Where(approved = true; AND MessageTypeID =4))
where (db.fAccesses.Where(fa => fa.ForumID == m.Forum.ForumID && db.uGroupUsers.Where(ua => ua.GroupID == fa.UserGroupID && ua.UserID == UserID).Any()).Any())
select m;


any suggestions please.

Thanks.
Posted
Updated 6-Jul-11 22:08pm
v2

LINQPad[^] allows you to convert LINQ queries to SQL queries.

Your query probably looks something like:

<pre lang="sql">select count(1) from ForumMessages fm
inner join fAccessess a on fm.ForumID = a.ForumID
inner join uGroupUsers ua on a.UserGroupID = ua.GroupID
where fm.approved = 1 and fm.MessageTypeID = 4 and ua.UserID = "UserID"



Although you linq query should be easier written as followed:
C#
var data = 
from m in db.ForumMessages
join fa in db.fAccesses on m.Forum.ForumID equals fa.ForumID
join ua in db.uGroupUsers on fa.UserGroupID equals ua.UserGroupID
where m.approved && m.MessageTypeID == 4 && ua.UserID == UserID;
 
Share this answer
 
v2
Comments
Bob_shekar 7-Jul-11 4:53am    
Thanks for your response.
Well, LINQ is virtually SQL in how it looks, but, is this LINQ operating on your database ? Do you not know SQL ?

select * from ForumMessages fm inner join fAccessess a on fm.ForumID = a.ForumID where fm.approved = 1 and fm.MessageTypeID = 4

That's as far as I can get. you're better off just looking at the DB and writing the SQL, instead of trying to convolute it from this. For starters, m seems to have objects as properties, I have no idea how that maps to your DB.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900