I have table with 10 000 elements.
IQuerable<IEntity> query = dataRep.Get<IEntity>()
.Query();
I need to get the index(rownum) of selected obj without getting all table items
var obj = query.Where( x => x.Name == "testName")
.FirstOrDefault();
The simple sql work fine :
select name, id, r from
(
select name, id, rownum r from collections order by id
) where name = 'testName';
How do this in Linq to NHibernate ?
Edit:
I tried add to IEntity class property RowNumber and mapping this on hbm as
<property name="RowNumber" formula="rownum" />
But after
var index = query.Where( x => x.Name == "testName")
.Select( x => x.RowNumber)
.FirstOrDefault();
Get always 1 value
Can you not just filter the query directly?
IQuerable<IEntity> query = dataRep.Get<IEntity>()
.Query()
.FirstOrDefault(x => x.Name == "testName");
Edit:
To get the item you can project into an anonymous type:
var query = (from data in dataRep.Get<IEntity>().Query()
where Name == "testName"
select new
{
id = data.id,
rowNumber = data.rowNumber
}).FirstOrDefault();
Related
I'm trying to get all active tasks from an employee by QueryOver and RowCount with this code:
var id = 1;
var activeTasks = Session
.QueryOver<Employee>()
.Where(emp => emp.id == id)
.JoinQueryOver<Tasks>(emp => emp.Tasks, JoinType.InnerJoin)
.Where(task => task.State == TaskState.Active)
.RowCount();
If there is no employee with the specified id, RowCount() returns 0.
The thing is I need to know if the employee didn't exist or if there was no active tasks.
I could do this with 2 queries where I would first get the employee and check for null, then query for the tasks. But ideally if it's possible I would want this to go all in one query.
You can do it much simpler with LINQ to NHibernate:
var employeeWithActiveTaskCount = session
.Query<Employee>()
.Where(e => e.Id == id)
.GroupJoin(
session
.Query<Task>()
.Where(t => t.State == TaskState.Active),
e => e.Id,
t => t.Employee.Id,
(e, t) => new { Employee = e, Tasks = t })
.Select(et => new
{
EmployeeId = et.Employee.Id,
TaskCount = et.Tasks.Count()
});
This will return only existing employees with appropriate number of tasks. In case the employee doesn't exist, empty collection is returned. The query generated by this looks as follows:
select employee0_.Id as col_0_0_, (select cast(count(*) as INT)
from [Task] task1_ where task1_.State=?
and (task1_.Employee_id=employee0_.Id
or (task1_.Employee_id is null)
and (employee0_.Id is null)))
as col_1_0_ from [Employee] employee0_ where employee0_.Id=?
I'm trying to use the value of signerID to be part of the where clause in another query, but getting red underline with error: "delegate does not take 1 argument". Basically I want to use the value of signerID query (value = 15) in the where clause of the query allDepartments. Here's the code:
using (dbPSREntities10 myEntities = new dbPSREntities10())
{
int theStatus = Convert.ToInt32(ddlChangeStatus.SelectedValue);
string userName = HttpContext.Current.User.Identity.Name;
var signerID = (from refAuthSigner in myEntities.refAuthSigners.Where(x => x.NetworkUsername == userName)
select new
{
refAuthSignerID = refAuthSigner.refAuthSignerID
}); <---- here I should have the value 15
var allDepartments = (from tbProject in myEntities.tbProjects.Where(x => x.refAuthSignerID == signerID)<-- and I want to use the value of signerID (15) here
Is this way off? I can hardcode 15 in place of signerID in the where clasue and everything works fine. Thanks in advance!
I suggest you to join tables on singer id:
var allDepartments =
from p in myEntities.tbProjects
join s in myEntities.refAuthSigners
on p.refAuthSignerID equals s.refAuthSignerID
where s.NetworkUsername == userName
select p;
This query will select only those project which have singer id equal to id of singer with provided name.
Of course, you can do all in two queries - get id by first query, and them use id in second query:
int singerId = myEntities.refAuthSigners
.Where(s => s.NetworkUsername == userName)
.Select(s => s.refAuthSignerID)
.FirstOrDefault(); // execute first query
if (singerId == 0)
return; // singer not found
// build second query
var allDepartments = myEntities.tbProjects
.Where(p => p.refAuthSignerID == signerID);
How to change the following linq query to select another field value Field<int>("data_entry"),i want to select multiple fields .
var a = DF_Utilities.GetAvailableTasks(empnum, 1).AsEnumerable().Where(
p => p.Field<int>("task_code") == int.Parse(drpTasks.SelectedValue)).Select(p => p.Field<int>("cand_num")).First();
p.Field<int>("cand_num"),Field<int>("data_entry")
instead of p.Field<int>("cand_num")
You can use anonymous type:
var a = DF_Utilities.
GetAvailableTasks(empnum, 1).
AsEnumerable().
Where(p => p.Field<int>("task_code") == int.Parse(drpTasks.SelectedValue)).
Select(p => new
{
candNum = p.Field<int>("cand_num"),
dataEntry = p.Field<int>("data_entry")
}).
First();
I'm using LINQ on an IQueryable returned from NHibernate and I need to select the row with the maximum value(s) in a couple of fields.
I've simplified the bit that I'm sticking on. I need to select the one row from my table with the maximum value in one field.
var table = new Table { new Row(id: 1, status: 10), new Row(id: 2, status: 20) }
from u in table
group u by 1 into g
where u.Status == g.Max(u => u.Status)
select u
This is incorrect but I can't work out the right form.
BTW, what I'm actually trying to achieve is approximately this:
var clientAddress = this.repository.GetAll()
.GroupBy(a => a)
.SelectMany(
g =>
g.Where(
a =>
a.Reference == clientReference &&
a.Status == ClientStatus.Live &&
a.AddressReference == g.Max(x => x.AddressReference) &&
a.StartDate == g.Max(x => x.StartDate)))
.SingleOrDefault();
I started with the above lambda but I've been using LINQPad to try and work out the syntax for selecting the Max().
UPDATE
Removing the GroupBy was key.
var all = this.repository.GetAll();
var address = all
.Where(
a =>
a.Reference == clientReference &&
a.Status == ClientStatus.Live &&
a.StartDate == all.Max(x => x.StartDate) &&
a.AddressReference == all.Max(x => x.AddressReference))
.SingleOrDefault();
I don't see why you are grouping here.
Try this:
var maxValue = table.Max(x => x.Status)
var result = table.First(x => x.Status == maxValue);
An alternate approach that would iterate table only once would be this:
var result = table.OrderByDescending(x => x.Status).First();
This is helpful if table is an IEnumerable<T> that is not present in memory or that is calculated on the fly.
You can also do:
(from u in table
orderby u.Status descending
select u).Take(1);
You can group by status and select a row from the largest group:
table.GroupBy(r => r.Status).OrderByDescending(g => g.Key).First().First();
The first First() gets the first group (the set of rows with the largest status); the second First() gets the first row in that group.
If the status is always unqiue, you can replace the second First() with Single().
Addressing the first question, if you need to take several rows grouped by certain criteria with the other column with max value you can do something like this:
var query =
from u1 in table
join u2 in (
from u in table
group u by u.GroupId into g
select new { GroupId = g.Key, MaxStatus = g.Max(x => x.Status) }
) on new { u1.GroupId, u1.Status } equals new { u2.GroupId, Status = u2.MaxStatus}
select u1;
What about using Aggregate?
It's better than
Select max
Select by max value
since it only scans the array once.
var maxRow = table.Aggregate(
(a, b) => a.Status > b.Status ? a : b // whatever you need to compare
);
More one example:
Follow:
qryAux = (from q in qryAux where
q.OrdSeq == (from pp in Sessao.Query<NameTable>() where pp.FieldPk
== q.FieldPk select pp.OrdSeq).Max() select q);
Equals:
select t.* from nametable t where t.OrdSeq =
(select max(t2.OrdSeq) from nametable t2 where t2.FieldPk= t.FieldPk)
Simply in one line:
var result = table.First(x => x.Status == table.Max(y => y.Status));
Notice that there are two action.
the inner action is for finding the max value,
the outer action is for get the desired object.
I'm have a SQL statement which I am trying to transform in a LINQ statement...
SELECT DISTINCT mc.*
FROM ManufractorCategories mc
WHERE mc.Active = 'true'
AND mc.Folder = 'false'
AND (mc.Id not in (SELECT Category_id FROM Manufractor_Category
WHERE Manufractor_id = 3));
That's my last, not working LINQ statement
(IQueryable<object>)db.ManufractorCategories
.Where(o => o.Active == active)
.Where(o => o.Folder == folder)
.Select(i => new { i.Id, i.Folder }).Except(db.Manufractor_Categories.Where(t => t.Manufractor_id == id).Select(t => new { t.Category_id })).Distinct();
I've tried the whole Sunday on that, but the Except statement won't work.
Thanks in advances for any help!
The Except method requires two sets of the same type - this means that you would have to select objects of type ManufractorCategory in the nested query as well as in the outer query - then it would select all categories that are in the first one and not in the second one.
An easier alternative is to use the Contains method to check whether the current ID is in a list of IDs that you want to filter. The following should work:
var q =
db.ManufractorCategories
.Where(o => o.Active == active)
.Where(o => o.Folder == folder)
.Select(i => new { i.Id, i.Folder })
.Where(o =>
!db.Manufractor_Categories
.Select(t => t.Manufractor_id)
.Contains(o.Id)
.Distinct();
And a simplified version using query syntax:
var q =
from o in db.ManufractorCategories
where o.Active == active && o.Folder == folder &&
db.Manufractor_Categories
.Select(t => t.Manufractor_id)
.Contains(o.Id)
select new { i.Id, i.Folder };
The Except statement is going to get a list of objects with the Category_id property. However, you're query has a result that contains objects with the Id and Folder properties. The query will most likely be unable to see where these objects are equal, and so, the Except clause won't take effect.