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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:08:16+00:00 2026-05-25T17:08:16+00:00

I’m just getting started with LINQ and I’m trying to select and return a

  • 0

I’m just getting started with LINQ and I’m trying to select and return a product price from database, like so:

public int GetPricePerKg(Product prod)
{
    var result = from p in dc.Products
                 where p.pk_product_id == prod.pk_product_id
                 select p.product_price_kg;
    return result;
}

This gives me the error:

Cannot implicitly convert type ‘System.Linq.IQueryable<int?>‘ to ‘int‘

What’s the best way to handle this? I need the price (which is an int in this case) and do some calculations with it in another place

  • 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-25T17:08:17+00:00Added an answer on May 25, 2026 at 5:08 pm

    Well, you’ve selected a query – that could in theory match 0, 1 or more records. What do you want to do in each situation? Also, it looks like product_price_kg is int? – what do you want to do if it’s null?

    You might want:

    public int GetPricePerKg(Product prod)
    {
        return dc.Products.Where(p => p.pk_product_id == prod.pk_product_id)
                          .Select(p => p.product_price_kg)
                          .Single()
                          .Value;
    }
    

    … but that will throw an exception if either there isn’t exactly one matching product or the price property is null.

    You can still use a query expression if you want – but I tend not to for simple cases where you then want to add a method call at the end. The query expression equivalent is:

    return (from p in dc.Products
            where p.pk_product_id == prod.pk_product_id
            select p.product_price_kg).Single().Value;
    

    Alternatively, you can use the version of Single() which takes a predicate, like this:

    return dc.Products.Single(p => p.pk_product_id == prod.pk_product_id)
                      .product_price_kg.Value;
    

    There are no doubt other variations – they’ll all do the same thing – pick whichever one you find most readable.

    EDIT: Other options instead of Single are:

    • First (cope with 1 or more)
    • FirstOrDefault (cope with 0, 1 or more)
    • SingleOrDefault (cope with 0 or 1)

    For the OrDefault versions you’d need to work out what to do if you didn’t match any products.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from

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.