Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:58:13+00:00 2026-05-22T19:58:13+00:00

I am having an issue with this query: var idsToFetch = new int[] {1,

  • 0

I am having an issue with this query:

var idsToFetch = new int[] {1, 2};

var q = from t in session.Query<Thing>()
        where idsToFetch.Contains(t.Id)
        let lastTask = (from task in session.Query<ReportTask>()
                       where task.Thang.Id == t.Id
                       orderby task.Id descending
                       select task).FirstOrDefault()
        select new {
            Id = t.Id,
            Errors = lastTask.Results.Sum(r => r.Errors)
        };

var errors = q.FirstOrDefault().Errors;

In English:

Please, for these "Thangs", give me their last total error count.

With the models:

public class Thing
{
    public virtual int Id { get; set; }
}

public class ReportTask
{
    public virtual int Id { get; set; }
    public virtual Thing Thang { get; set; }
    public virtual ICollection<ReportResult> Results { get; set; }
}

public class ReportResult
{
    public virtual int Id { get; set; }
    public virtual ReportTask Task { get; set; }
    public virtual int Errors { get; set; }
}  

This is a simulation of the exact issue I’m facing with my real world project. I get this error when trying to assign errors:

Exception of type
‘Antlr.Runtime.NoViableAltException’
was thrown.
[.FirstOrDefault[<>f_AnonymousType22[[System.Int32,
mscorlib, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Int32,
mscorlib, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089]]](.Select[<>f__AnonymousType1
2[[TestWeb.CSharp.Models.Thing,
TestWeb.CSharp, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null],[TestWeb.CSharp.Models.ReportTask,
TestWeb.CSharp, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null]],<>f
_AnonymousType22[[System.Int32,
mscorlib, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.Int32,
mscorlib, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089]]](.Select[TestWeb.CSharp.Models.Thing,<>f__AnonymousType1
2[[TestWeb.CSharp.Models.Thing,
TestWeb.CSharp, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null],[TestWeb.CSharp.Models.ReportTask,
TestWeb.CSharp, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null]]](.Where[TestWeb.CSharp.Models.Thing](NHibernate.Linq.NhQueryable1[TestWeb.CSharp.Models.Thing],
Quote((t, ) =>
(.Contains[System.Int32](p1, t.Id,
))), ), Quote((t, ) => (new
<>f__AnonymousType1
2(t,
.FirstOrDefault[TestWeb.CSharp.Models.ReportTask](.OrderByDescending[TestWeb.CSharp.Models.ReportTask,System.Int32](.Where[TestWeb.CSharp.Models.ReportTask](NHibernate.Linq.NhQueryable1[TestWeb.CSharp.Models.ReportTask],
Quote((task, ) =>
(Equal(task.Thang.Id, t.Id))), ),
Quote((task, ) => (task.Id)), ), ),
))), ),
Quote((<>h__TransparentIdentifier0, )
=> (new <>f__AnonymousType2
2(<>h_TransparentIdentifier0.t.Id,
OrElse(Equal(<>h
_TransparentIdentifier0.lastTask,
NULL),
Equal(<>h_TransparentIdentifier0.lastTask.Results,
NULL)) ? p2 :
.Sum[TestWeb.CSharp.Models.ReportResult](<>h
_TransparentIdentifier0.lastTask.Results,
(r, ) => (r.Errors), ), ))), ), )]

I’ve tried many other formats for this query, including:

from x in x
where y.Contains(x.Id)
select ((from ... subquery).FirstOrDefault())

This gets me farther, in that a query like this is made in SQL:

select (select 
            thing0_.Id, 
            (select cast(sum(results2_.Errors) as INTEGER) 
             from "ReportResult" results2_ 
             where reporttask1_.Id=results2_.ReportTask_id) 
        from "ReportTask" reporttask1_ 
        where reporttask1_.Thang_id=thing0_.Id 
        order by reporttask1_.Id desc) as col_0_0_ 
from "Thing" thing0_ 
where thing0_.Id in (1, 2) 
limit 1

This is syntactically incorrect and in SQLLite, I get:

SQLite error
only a single result allowed for a SELECT that is part of an expression

In SQL Server 2008 (my real DB I use), it’s a very similar error about subqueries not allowing more than one expression.

So I feel like I’m really close to the answer but I just can’t quite get it to work. Any ideas?

PS. In LINQPad using Linq-2-SQL this query works fine.

PSS. I can’t do this query using Criteria because I suck at Criteria. Bonus points to someone who gets this baby to work in Criteria… I’m totally open to that if LINQ isn’t possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:58:14+00:00Added an answer on May 22, 2026 at 7:58 pm

    I ended up making my SQL query into a database View and just querying against that to prevent me from pulling my hair out with NHibernate’s LINQ implementation. If anyone wants to come back and show me the “proper” way to do this, please do!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue getting a LINQ query to work. I have this XML:
I am having a similar issue to this person . The primary difference being
I'm having an issue with a query that currently uses LEFT JOIN weblog_data AS
I'm using NHibernate 2.1.2.400, and I'm having an issue with a an ISQLQuery query.
I have a query hitting EF4 using STEs and I'm having an issue with
I am having an issue with a simple form uploading script. On this upload
I'm having some issue with embedding Traditional Chinese characters from the Arial Unicode MS
I'm having some weird issues with Xcode, and this is pretty much impossible to
I'm having an issue with my regex. I want to capture <% some stuff
I'm having an issue with a Flash/Flex erroring in Firefox but not IE. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.