Nhibernate QueryOver WithSubquery - fluent-nhibernate

I have a detached query that projects 2 fields:
var detached = session.QueryOver.Of<ClassA>().
select
(
Projections1.....X
Projections2.....Y
)
var result = session.QueryOver<ClassB>()
.WithSubquery
.WhereProperty(p => p.X)
.In(detached)
.List();
What i would like to do is this:
select * from tableA a
where (a.x, a.y) in (select x, y from tableB b)
What i want is to have two fields in the nested part, but how am I adding two fields in my WhereProperty ??

Related

How can i write Linq of this query ? ( Linq C#)

Sql Query is :
select T1.SellerId , count(T2.productId)
from
tbl_1 T1
left join
tbl_2 T2
on T1.SellerId = T2.sellerId
where VisitorUserId = 'bf8581b04429fdf56c6ebc'
group by T1.SellerId
I Need This SQL Query in Linq C#.
Assuming that VisitorUserId is a column of tbl_1 and assuming you use ORM like Entity Framework with navigation properties for foreign keys relationships it should be smth like this:
context.tbl_1
.Where(i => i.VisitorUserId = 'bf8581b04429fdf56c6ebc')
.Select(i => new { SellerId = i.SellerId, ProductCount = i.tbl_2.Count() })

Linq in entity framework

I am having trouble with a select with a select.
The select query in () which is selecting the userid is not liked by the IDE. How do you accomplish a select within a select?
Dim newctx As teckModel.teckEntities
Dim pending = From c In newctx.my_aspnet_membership Where c.userId = (From c In newctx.my_aspnet_usersinroles Where c.roleId = 8 Select c.userId) Select c.Email, c.CreationDate, c.LastLoginDate
The equals operator assumes a single value, but the second linq statement is returning an IEnumerable. You should use .Contains() on the IEnumerable or further refine the subquery to return only a single entity.
var pending = from x in newctx.my_aspnet_membership where
(from y in newctx.my_aspnet_usersinroles where y.roleId==8 select y.userId).Contains(x.userId)
select new { x.Email, x.CreationDate, x.LastLoginDate };
You can either do a Contain or Any or you'll have to use FirstOrDefault
Where (From c In newctx.my_aspnet_usersinroles Where c.roleId = 8 Select c.userId).Contains(c.userId)
or
c.userId == (From c In newctx.my_aspnet_usersinroles Where c.roleId = 8 Select c.userId).FirstOrDefault()

The Translation of an SQL query to LinQ

Due to wierd request from a Customer, I have managed to figure out how to implement it
using SQL query, but I couldn't translate it into LinQ.
SELECT (SELECT count(*) FROM table1 where attribute1 like 'value1'),
(SELECT count(*) FROM table2 where attribute2 like 'value2')
What the translation of the query to LinQ?
You could just supply a predicate to the Count() function
var result = new {
Count1 = table1.Count(r => r.attribute1.Contains("value1")),
Count2 = table2.Count(r => r.attribute2.Contains("value2"))
};
var count1 = (from i in table1 where SqlMethods.Like(i.attribute1, "value1") select i).Count();
var count2 = (from i in table2 where SqlMethods.Like(i.attribute2, "value2") select i).Count();

php sql merge 2 queries

I have 2 queries one straight after the other:
$sql_result8 = mysql_query("SELECT * FROM properties WHERE c4='$id'", $db); $charges = mysql_num_rows($sql_result8);
$sql_result8 = mysql_query("SELECT * FROM vehicles WHERE c4='$id'", $db); $charges = $charges + mysql_num_rows($sql_result8);
What kind of query would I need to merge these? Some kind of JOIN? UNION?
SELECT * FROM properties p, vehicles v WHERE p.c4 = v.c4 AND p.c4 = '$id'
Try this
SELECT * FROM properties JOIN vehicles USING (c4) WHERE c4='$id'
If you want to just find out the number or rows returned from both queries and not show the actual columns, you can use this:
$sql_result8 = mysql_query(
"SELECT
( SELECT COUNT(*) FROM properties WHERE c4='$id' )
+ ( SELECT COUNT(*) FROM vehicles WHERE c4='$id' )
", $db );

Derived tables in Linq to SQL

I would like to implement this in Linq to SQL:
select * from (
select * from Orders) as A
Obviously this is a pointless example, but I just can't figure out how to do it!
Try something like this:
var subquery = from row in Orders select row;
var query = from row in subquery select row;
using (var dc = new NorthwindDataContext())
{
var A = from ordOuter in
from ordInner in dc.Orders
select ordInner
select ordOuter;
}