[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Build LINQ  with Sub-Select from Working SQL Query?

Posted on 2013-11-06
4
Medium Priority
?
768 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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

829 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