Solved

Build LINQ  with Sub-Select from Working SQL Query?

Posted on 2013-11-06
4
686 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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