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

  • SEARCH
  • Home
  • 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 6224431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:40:19+00:00 2026-05-24T08:40:19+00:00

I have the following linq query var result = from myTypes in context.MyTypes where

  • 0

I have the following linq query

var result = from myTypes in context.MyTypes
where
  ((myTypes .Prop1== "Test" ? 1 : 0) + 
  (myTypes .Prop2 == "Tester2" ? 1 : 0) + 
  (myTypes .Prop3 == "624642624000000000" ? 1 : 0) + 
  (myTypes .Prop4 == "TS166TH" ? 1 : 0) + 
  (myTypes .Prop5 == "1 Test Lane" ? 1 : 0)) >= 4
  select new {
  myTypes .Prop1,
  myTypes .Prop2,
  myTypes .Prop3,
  myTypes .Prop4,
  myTypes .Prop5,
  myTypes .OtherProp,
  myTypes .OtherTypeId
};

result.ToList();

This results in the following expected SQL which is what I want to achieve

SELECT 
[Extent1].[Prop1] AS [Prop1], 
[Extent1].[Prop2] AS [Prop2], 
[Extent1].[Prop3] AS [Prop3], 
[Extent1].[Prop4] AS [Prop4], 
[Extent1].[Prop5] AS [Prop5], 
[Extent1].[OtherProp] AS [OtherProp], 
[Extent1].[OtherTypeId] AS [OtherTypeId],

FROM [dbo].[MyType] AS [Extent1]
WHERE ((CASE WHEN ('Test' = [Extent1].[Prop1]) THEN 1 ELSE 0 END) + (CASE WHEN ('Tester2' = [Extent1].[Prop2]) THEN 1 ELSE 0 END) + (CASE WHEN ('624642624000000000' = [Extent1].[Prop3]) THEN 1 ELSE 0 END) + (CASE WHEN ('TS166TH' = [Extent1].[Prop4]) THEN 1 ELSE 0 END) + (CASE WHEN ('1 Test Lane' = [Extent1].[Prop5]) THEN 1 ELSE 0 END)) >= 4

However I want to return MyType not an anonymous type so I wrote the following

var result = from myTypes in context.MyTypes
where
  ((myTypes .Prop1== "Test" ? 1 : 0) + 
  (myTypes .Prop2 == "Tester2" ? 1 : 0) + 
  (myTypes .Prop3 == "624642624000000000" ? 1 : 0) + 
  (myTypes .Prop4 == "TS166TH" ? 1 : 0) + 
  (myTypes .Prop5 == "1 Test Lane" ? 1 : 0)) >= 4
  select myTypes; 

result.ToList();

Which I would expect to generate the same query but return my type. Instead I find it performs a massive recursive query (essentially querying every record in the table) As below.

SELECT 
[Extent1].[Prop1] AS [Prop1], 
[Extent1].[Prop2] AS [Prop2], 
[Extent1].[Prop3] AS [Prop3], 
[Extent1].[Prop4] AS [Prop4], 
[Extent1].[Prop5] AS [Prop5],
[Extent1].[OtherProp] AS [OtherProp],
[Extent1].[OtherTypeId] AS [OtherTypeId],
FROM [dbo].[MyType] AS [Extent1]
WHERE ((CASE WHEN ('Test' = [Extent1].[Prop1]) THEN 1 ELSE 0 END) + (CASE WHEN ('Tester2' = [Extent1].[Prop2]) THEN 1 ELSE 0 END) + (CASE WHEN ('624642624000000000' = [Extent1].[Prop3]) THEN 1 ELSE 0 END) + (CASE WHEN ('TS166TH' = [Extent1].[Prop4]) THEN 1 ELSE 0 END) + (CASE WHEN ('1 Test Lane' = [Extent1].[Prop5]) THEN 1 ELSE 0 END)) >= 4
GO

SELECT 
[Extent1].[OtherTypeId] AS [OtherTypeId], 
[Extent1].[OtherTypeProp] AS [OtherTypeProp], 
FROM [dbo].[OtherTypes] AS [Extent1]
GO

-- Region Parameters
DECLARE EntityKeyValue1 BigInt = 1
-- EndRegion
SELECT 
[Extent1].[Prop1] AS [Prop1], 
[Extent1].[Prop3] AS [Prop2], 
[Extent1].[Prop3] AS [Prop3], 
[Extent1].[Prop4] AS [Prop4], 
[Extent1].[Prop5] AS [Prop5], 
[Extent1].[OtherProp] AS [OtherProp], 

WHERE [Extent1].[OtherTypeId] = @EntityKeyValue1
GO

-- Region Parameters
DECLARE EntityKeyValue1 BigInt = 2
-- EndRegion
SELECT 
[Extent1].[Prop1] AS [Prop1], 
[Extent1].[Prop3] AS [Prop2], 
[Extent1].[Prop3] AS [Prop3], 
[Extent1].[Prop4] AS [Prop4], 
[Extent1].[Prop5] AS [Prop5], 
[Extent1].[OtherProp] AS [OtherProp], 

WHERE [Extent1].[OtherTypeId] = @EntityKeyValue1
GO

It looks like It’s getting all the fk types for EVERY record and ignoring the where clause?

Can anyone explain what I’m doing wrong here and why are the two generated query’s different?

  • 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-24T08:40:19+00:00Added an answer on May 24, 2026 at 8:40 am

    OK That was embarrassing.

    I was using LinqPad to test the query and was calling a Dump() on the ToList() result. Dump() enumerates all the properties which caused a cyclic enumeration through all related records in the db. Where as a Dump on the anonymous types just list all the properties and no entity Navigation properties cause enumeration.

    A note to all using LinqPad’s Dump() method on Entity’s in future. Apologies to all for wasting time. School boy error.

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

Sidebar

Related Questions

I have the following query in Linq: var query = from question in context.Questions.Include(QAnswers)
I have the following LINQ query: var queryGroups = (from p in db.cl_contact_event select
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I have the following Nhibernate LINQ query: var query = from c in session.Query<Customer>()
I have the following XML LINQ query from my XDocument. var totals = (from
I have following query: var result = ( from role in db.Roles join user
I have the following LINQ query: var aKeyword = ACT; var results = from
i have the following LINQ statement: var query =(from item in _itemRepository.FindAll() where item.Id
I have the following LINQ query - var data = (from req in db.tblRequirements
I have the following LINQ expression that's not returning the appropriate response var query

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.