0

I have a VB.NET LINQ query with a subquery which intention is to select one, best matching, item.

    Dim queryOut = (From x In lstIOsMainStructuresCrossed
                        Let SearchedVersionID = (From qecm In lstExpectedCountMatch
                        Join qcm In lstCountMatch
                        On qecm.Version_id Equals qcm.Version_id And qecm.Expected_count_match Equals qcm.Count_match
                        Where qcm.Main_str_id = qecm.Main_str_id AndAlso qcm.IO_id = x.IO_id
                        Order By qcm.Count_match Descending
                        Select qcm.Version_id).FirstOrDefault
                    Select x.IO_id, x.Main_structure_id, SearchedVersionID).ToList

The problem is FirstOrDefault kills the query efficiency. I cannot use Take(1) as it does not produce a value.

Is there a way to get TOP 1 without FirstOrDefault method?

Sandokan
  • 23
  • 4
  • Why you have 3 lists at all? – Tim Schmelter Mar 06 '17 at 14:44
  • @Tim Schmelter They are holding product definitions and order details. The query is used to match product definition with order. They are LINQ to SQL stored procedures results. – Sandokan Mar 06 '17 at 14:50
  • 2
    The problem is not `FirstOrDefault`. You really need `Join` or `GroupJoin` here. – Ivan Stoev Mar 06 '17 at 15:03
  • I don't really understand how Join or GropupJoin can help to select top record. FirstOrDefault is a problem for me as it causes efficiency is very poor when this method is used. – Sandokan Mar 06 '17 at 18:04

0 Answers0