Request for LINQ query - linq

I have a sql query. Please someone help me to convert it into LINQ.
select * from ProductOrder where OrderId != (Select OrderId from OrderEvents) and MemberId=2
I write this code. But tis is not working. please help me.
from order in db.ProductOrder
from cancel in db.OrderEvents
where order.MemberId == MemberId
where order.OrderId != cancel.OrderId
//orderby order.OrderId descending
select order

ProductOrder.Where(x=>x.OrderId != OrderEvents.OrderId && x.MemberId ==2)

Related

Converting order by case to Linq statement

I have a query such as;
select * from TABLE_DEF_ROLE order by case when RoleName = 'Select All' then 0 else 1 end, RoleName
I have checked the related questions and answers but still couldn't figure it out. How would I translate it into a linq statement?
You can try this:
var roles = from r in db.Roles
orderby r.RoleName == "Select All" ? 0 : 1 descending
select r;
(Roles entity refers to your table)

how to do subquery in LINQ to SQL

I'm a bit stuck on this. Basically I want to do something like the following SQL query in LINQ to SQL:
select Image from dbo.Employee where ID in(Select ID from dbo.Department where IsActive=1)
Any help would be gratefully received.
Thanks.
You can force the point with a subquery as in the question #IswantoSan links to (how to do subquery in LINQ) but, without knowing more about your entity relationships, why wouldn't simply use a join here?
from e in Employees
join d in Departments on e.ID equals d.ID
where d.IsActive
select e.Image
select Image from dbo.Employee
.Where(u =>
dbo.Employee
.Where(x => u.IsActive = 1)
.Select(x.ID)
)
Is the query that will work for you.

LINQ - count from select with join with no group by

Linq is brand new to me so I apologize if this is really stupid.
I am trying to get the count from a multi-table join with where clause, without group by. I've seen examples of group by and will resort to that if need be, but I am wondering if there is a way to avoid it. Is sql my query would look something like this;
SELECT Count(*)
FROM plans p
JOIN organizations o
ON p.org_id = o.org_id
AND o.deleted IS NULL
JOIN orgdata od
ON od.org_id = o.org_id
AND od.active = 1
JOIN orgsys os
ON os.sys_id = od.sys_id
AND os.deleted IS NULL
WHERE p.deleted IS NULL
AND os.name NOT IN ( 'xxxx', 'yyyy', 'zzzz' )
What's the best way to get this?
All you need is to call Count(). You're only counting the number of results. So something like:
var names = new[] { "xxxx", "yyyy", "zzzz" };
var query = from plan in db.Plans
where plan.Deleted == null
join organization in db.Organizations
on plan.OrganizationId equals organization.OrganizationId
where organization.Deleted == null
join orgData in db.OrganizationData
on organization.OrganizationId equals orgData.OrganizationId
where orgData.Active == 1
join os on db.OrganizationSystems
on orgData.SystemId equals os.SystemId
where os.Deleted == null &&
!names.Contains(os.Name)
select 1; // It doesn't matter what you select here
var count = query.Count();

MVC3 select int value through LINQ

I am trying to get customerId from Customer table.
However, when I try to make it, it has error said, 'Cannot implicitly convert type 'System.Linq.Iqueryable' to 'int''
How can I get customerId??
Thank you.
Here is my coding.
int customerId = from a in db.Customer
where a.userName == customerName
select a.customerId;
//it has error in here.
order.customerId = customerId;
It's possible that your query could return multiple rows. You need to tell LINQ that you only want the first (or a single) result:
int customerId = (from a in db.Customer
where a.userName == customerName
select a.customerId).First();
I like using the .Single() function when I know there should be only one row.
int customerId = (from a in db.Customer
where a.userName == customerName
select a.customerId).Single();
This throws an exception if more or less than one row is found, which is sometimes useful depending on your situation.
use it this way:
var customer = db.Customer.FirstOrDefault(c =>c.userName == customerName)
var id = customer.Id
Select returns an IEnumerable, so you can't convert it to int.
There is one more suggestion I can recommend for this question is to use First or default.
var customerId = (from a in db.Customer
where a.userName == customerName
select a.customerId).FirstOrDefault();
order.customerId = customerId; this should now work fine since it already knows it int

Can I use this query in the entity 4.0 framework?

I'm needing some syntax help with the following query please? I'd like to use an equivalent in the entity framework but I'm unsure of the syntax.
Can someone help me format this to work with the entity framework?
Thanks in advance.
Select * from (
SELECT [Member]
,[MemberGroup],
(SELECT [text]
FROM [umbracoNode]where [id] = [Member]) As MemberName,
(SELECT [text]
FROM [umbracoNode]where [id] = [MemberGroup]) As GroupName
FROM [cmsMember2MemberGroup]
) UG
where UG.MemberName is not null
order by UG.MemberName,
UG.GroupName
Try this:
var query =
from x in db.cmsMember2MemberGroup
join y in db.umbracoNode on x.Member equals y.id
let MemberName = y.text
where MemberName != null
join z in db.umbracoNode on x.MemberGroup equals z.id
let GroupName = z.text
orderby new { MemberName, GroupName }
select new
{
x.Member,
x.MemberGroup,
MemberName,
GroupName,
};
Is there a particular reason that this query is in this format?
Why are there not use of joins?
Need to really understand what you want here first and get into better sql before going to linq.

Resources