Solved

Build LINQ  with Sub-Select from Working SQL Query?

Posted on 2013-11-06
4
709 Views
Last Modified: 2016-02-10
This SQL query works perfectly. My LINQ returns null.

[SQL]
Select c.Name, (select Name from Category Where CategoryID = e.CategorySubID)
From Category c
Join Expense e on c.CategoryID = e.CategoryID

[LINQ]
from c in context.Categories
join e in context.Expenses on c.CategoryID equals e.CategoryID
let j = new ExpenseViewModel
{
   Category = c,
   Expense = e,
 // returns null, no errors
   CatSubName = (from x in context.Categories
                               where c.CategoryID == e.CategorySubID select c.Name ).FirstOrDefault() }
group j by j.Category.Name into g
select new Group<string, ExpenseViewModel>
{
    Key = g.Key,
    Values = g.OrderBy(s => s.CatSubName)
}
).ToList();

[ExpenseViewModel]
    public class ExpenseViewModel
    {
        public Expense Expense { get; set; }
        public Category Category { get; set; }
        public Object CatSubName { get; set; }
    }
0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39628119
Your SQL isn't performing a Group By, so why are you doing so in your LINQ?
0
 

Author Comment

by:WorknHardr
ID: 39628439
SQL is just for testing the Select. I know they don't match. I do need the LINQ group etc...
0
 

Accepted Solution

by:
WorknHardr earned 0 total points
ID: 39636491
Solution:

[ExpenseViewModel]
...
public string CatSubName { get; set; }
...

[LINQ]
...
let j = new ExpenseViewModel
{
  Category = c,
  Expense = e,
  CatSubName = context.Categories.Where(x => x.CategoryID == e.CategorySubID).Select(x => x.Name).FirstOrDefault() }
...
0
 

Author Closing Comment

by:WorknHardr
ID: 39647406
Figured it out myself...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Web page design problem 3 41
Windows Universal App 22 58
Issue in displaying table columns on selection of dropdown 2 27
Javascript function 3 11
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question