This is a migrated thread and some comments may be shown as answers.

Is it possible to do TOP 1 subquery using LINQ at server side?

2 Answers 290 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alex
Top achievements
Rank 1
Alex asked on 03 Feb 2011, 03:03 PM
I have 2 classes

class Order
{
   public int ID {get;set;}
   public IList<Image> Images {get;set;}
}
  
class Image
{
  public int ID {get;set;}
  public Order Order {get;set;}
  public int DisplayOrder {get;set;}
}

now I need to build query which will return me list of orders with their first image if it exists, I'm trying to run something like
query.Select(o => new
{
  ID=o.ID,
  DefaultImage = o.Images.SingleOrDefault(i=>i.DisplayOrder==0)
});

but this doesn't worked. It throws exception

System.NotSupportedException : Execution of 'System.Linq.Enumerable:SingleOrDefault(IEnumerable`1,Func`2)' on the database server side currently not implemented.

tried different ways to do this (for example o.Images.OrderBy(i=>i.DisplayOrder).FirstOrDefault()) - but it gave the same error.

Is there any way to accomplish my task by running one query on server? I guess I could use left join - but what will be best way performance wise?

Thanks!

2 Answers, 1 is accepted

Sort by
0
Alex
Top achievements
Rank 1
answered on 03 Feb 2011, 03:16 PM
I did this using left join, but it looks like it is processed mostly on client. I looked via SQL Profiler and I can see how ORM first executes SELECT to find all records from my right table... This is not acceptable due performance reasons. Please suggest.
0
Jordan
Telerik team
answered on 07 Feb 2011, 11:14 AM
Hi Alex,

Please, find the answer to your question in your support ticket on the same subject.

Best wishes,
Jordan
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
LINQ (LINQ specific questions)
Asked by
Alex
Top achievements
Rank 1
Answers by
Alex
Top achievements
Rank 1
Jordan
Telerik team
Share this question
or