Solved

Build LINQ  with Sub-Select from Working SQL Query?

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
Response.Redirect Open new Form/tab/page? 5 20
Round up to 100% in .NET 10 45
Adjust the position 3 13
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now