Click here to Skip to main content
15,914,481 members
Home / Discussions / C#
   

C#

 
GeneralRe: LINQ Join Result Is Null Pin
MadDashCoder24-Jul-16 18:24
MadDashCoder24-Jul-16 18:24 
AnswerRe: LINQ Join Result Is Null Pin
Richard Deeming24-Jul-16 22:31
mveRichard Deeming24-Jul-16 22:31 
I suspect the problem is that the category ID you've passed in doesn't exist. As a result, the SingleOrDefault method returns null, and you then try to retrieve the _categoryName property from that null reference.

If you want the code to fail if the category ID doesn't exist, change the code to use Single instead. Otherwise, you'll need to test the value returned from SingleOrDefault to see if it's null.

You can combine the Where and Single[OrDefault] calls, as both Single[OrDefault] and First[OrDefault] have overloads which accept a filter.

And there's no need to create the two List<T> instances at the start, as you immediately overwrite them on the following lines.

C#
List<Product> _products = psc.FindAllProducts();
List<Category> _categories = psc.FindAllCategories();

var _productCategories = 
    from c in _categories
    join p in _products
    on c.ID equals p.CategoryID
    select new {
        p.ID, p.Name, p.Price,
        p.Quantity, p.CategoryID,
        _categoryID = c.ID,
        _categoryName = c.Name
    };

var selectedProductCategory = _productCategories.SingleOrDefault(x => x._categoryID.ToString() == id);
var selectedCat = selectedProductCategory == null ? null : selectedProductCategory._categoryName;

// Or, with C# 6:
var selectedCat = _productCategories.SingleOrDefault(x => x._categoryID.ToString() == id)?._categoryName;

// Or, to throw an exception if the category doesn't exist:
var selectedCat = _productCategories.Single(x => x._categoryID.ToString() == id)._categoryName;


Also, are you sure there's only ever one product in a given category? If there's more than one, Single[OrDefault] will throw an exception.

And I'm not sure why you're passing the category ID as a string, when it's defined as an int in the model?



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: LINQ Join Result Is Null Pin
MadDashCoder25-Jul-16 4:51
MadDashCoder25-Jul-16 4:51 
QuestionWhat is the correct code to invoke subscribers for an event? Pin
User 1106097923-Jul-16 21:16
User 1106097923-Jul-16 21:16 
AnswerRe: What is the correct code to invoke subscribers for an event? Pin
BillWoodruff23-Jul-16 21:31
professionalBillWoodruff23-Jul-16 21:31 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
User 1106097923-Jul-16 21:54
User 1106097923-Jul-16 21:54 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
BillWoodruff23-Jul-16 22:03
professionalBillWoodruff23-Jul-16 22:03 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
Richard MacCutchan24-Jul-16 1:31
mveRichard MacCutchan24-Jul-16 1:31 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
BillWoodruff24-Jul-16 2:59
professionalBillWoodruff24-Jul-16 2:59 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
Richard MacCutchan24-Jul-16 4:28
mveRichard MacCutchan24-Jul-16 4:28 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
Richard Deeming24-Jul-16 22:15
mveRichard Deeming24-Jul-16 22:15 
AnswerRe: What is the correct code to invoke subscribers for an event? Pin
Pete O'Hanlon23-Jul-16 23:43
mvePete O'Hanlon23-Jul-16 23:43 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
User 1106097923-Jul-16 23:53
User 1106097923-Jul-16 23:53 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
Pete O'Hanlon24-Jul-16 5:51
mvePete O'Hanlon24-Jul-16 5:51 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
BillWoodruff24-Jul-16 23:04
professionalBillWoodruff24-Jul-16 23:04 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
Richard Deeming25-Jul-16 0:05
mveRichard Deeming25-Jul-16 0:05 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
User 1106097925-Jul-16 0:30
User 1106097925-Jul-16 0:30 
SuggestionRe: What is the correct code to invoke subscribers for an event? PinPopular
Maarten197725-Jul-16 3:16
Maarten197725-Jul-16 3:16 
GeneralRe: What is the correct code to invoke subscribers for an event? Pin
User 1106097925-Jul-16 3:58
User 1106097925-Jul-16 3:58 
QuestionTime of action in [Date] [Time] [Action] Pin
mmiklauz23-Jul-16 10:01
mmiklauz23-Jul-16 10:01 
AnswerRe: Time of action in [Date] [Time] [Action] Pin
Mycroft Holmes23-Jul-16 14:20
professionalMycroft Holmes23-Jul-16 14:20 
AnswerRe: Time of action in [Date] [Time] [Action] Pin
BillWoodruff23-Jul-16 15:41
professionalBillWoodruff23-Jul-16 15:41 
QuestionSave Physical File using MySQL Pin
Jassim Rahma22-Jul-16 23:29
Jassim Rahma22-Jul-16 23:29 
AnswerRe: Save Physical File using MySQL Pin
OriginalGriff23-Jul-16 0:31
mveOriginalGriff23-Jul-16 0:31 
GeneralRe: Save Physical File using MySQL Pin
Jassim Rahma23-Jul-16 7:19
Jassim Rahma23-Jul-16 7:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.