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 6689389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:30:49+00:00 2026-05-26T05:30:49+00:00

I’ve got a table that looks like this: Foo FooId : int (PK) BarId

  • 0

I’ve got a table that looks like this:

Foo
 FooId : int (PK)
 BarId : int
 Baz   : bit
 etc.

It has other columns in it (etc.), but I have a specific query that I want to run that projects some statistics. The query in SQL would look like this:

SELECT 
 BarId, 
 SUM(CAST(Baz AS INT)) AS BazCount
 FROM Foo GROUP BY BarId;

So, I created a Presentation Model class to hold the data, so I can return it to the client.

public partial class FooStatistics
{
  public int BarId { get; set; }
  public int BazCount { get; set; }
}

I’m not 100% sure how to do the same query in LINQ and project it into this object, but I gave it a shot like this:

FooStatistics stats = (
    from f in ctx.Foo
    where <clauses here>
    group f by f.BarId
      into StatsGroup
      select new FooStatistics() {
        BarId = StatsGroup.Key,
        BazCount = StatsGroup.Sum(f => Int32.Parse(f.Baz.ToString()))
      }
    ).FirstOrDefault();

This blows up with the error:

LINQ to Entities does not recognize the method ‘Int32 Parse(System.String)’ method, and this method cannot be translated into a store expression.

So, I followed the advice given here:

LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression

I added this to my .edmx XML

<Function Name="ParseInt" ReturnType="Edm.Int32">
  <Parameter Name="value" Type="Edm.String" />
  <DefiningExpression>
     cast(value as Edm.Int32)
  </DefiningExpression>
</Function>

Then I added a partial class to define the method:

public partial class MyEntities
{
   [EdmFunction("MyEntities", "ParseInt")]
   public static Int32 ParseInt(string value)
   {
       return Int32.Parse(value);
   }
}

And I changed my LINQ to:

FooStatistics stats = (
    from f in ctx.Foo
    where <clauses here>
    group f by f.BarId
      into StatsGroup
      select new FooStatistics() {
        BarId = StatsGroup.Key,
        BazCount = StatsGroup.Sum(f => MyEntities.ParseInt(f.Baz.ToString()))
      }
    ).FirstOrDefault();

But this blows up with:

LINQ to Entities does not recognize the method ‘System.String ToString()’ method, and this method cannot be translated into a store expression.

So, I changed the function in the .edmx XML to this:

<Function Name="BoolToInt32" ReturnType="Edm.Int32">
   <Parameter Name="value" Type="Edm.Boolean" />
   <DefiningExpression>
      cast(value as Edm.Int32)
   </DefiningExpression>
</Function>

And I changed my static ParseInt to BoolToInt32 accordingly and I changed the LINQ to use that function, but now it blows up with:

The specified method ‘Int32 BoolToInt32(Boolean)’ cannot be translated into a LINQ to Entities store expression.

Am I close, or am I doing it totally wrong…?

Thanks in advance

  • 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-26T05:30:49+00:00Added an answer on May 26, 2026 at 5:30 am

    If Baz is a bit, you could add a where and then just use Count() as below

    FooStatistics stats = (
        from f in ctx.Foo
        where <clauses here>
           and f.Baz
        group f by f.BarId
          into StatsGroup
          select new FooStatistics() {
            BarId = StatsGroup.Key,
            BazCount = StatsGroup.Count()
          }
        ).FirstOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.