0

I am fairly new to SubSonic 3/Linq, and don't know if I'm missing something obvious, but I think I ran into a projection issue. I am trying to perform the most basic query, and getting back proper results only when I use anonymous types. The moment I swap the anonymous types with regular class types, I get all properties set to null/zero. I am crafting a DAL class library, so unfortunately anonymous types are not an option.

Snippet

using System;
using System.Linq;
using Fruits.Data;

namespace FruitTest {

    class Program {
        static void Main(string[] args) {

            var db = new FruitsDB();

            var fruits = from f in db.Fruits
                         select new FruitView {
                             MyFruitID = f.FruitID,
                             MyFruitName = f.FruitName,
                         };

            foreach (var f in fruits) {
                Console.WriteLine(f.MyFruitID + "\t" + f.MyFruitName);
            }

        }
    }

    public class FruitView {
        public int MyFruitID { get; set; }
        public string MyFruitName { get; set; }
    }

}

So this doesn't work (returns all nulls/zeros)

var fruits = from f in db.Fruits
             select new FruitView {
                 MyFruitID = f.FruitID,
                 MyFruitName = f.FruitName,
             };

This works as expected

var fruits = from f in db.Fruits
             select new {
                 MyFruitID = f.FruitID,
                 MyFruitName = f.FruitName,
             };

My problem is somewhat similar to this and this, only I am not even doing joins; just simple selects.

Any clues will be much appreciated.

Community
  • 1
  • 1
Daniel Liuzzi
  • 16,807
  • 8
  • 52
  • 57
  • BTW, I'm using the latest GitHub bits for both Core and LinqTemplates. – Daniel Liuzzi Nov 14 '09 at 12:51
  • Yes. Thats a bug I also encountered. Very irritating instead. The only way I got the result was by using a ForEach on the results to save them in a new list. – Yogesh Nov 15 '09 at 10:02
  • @Yogesh: Yes, that's the workaround I'm currently using. The problem with it is that you lose the lazy execution in the process :( – Daniel Liuzzi Nov 15 '09 at 10:41
  • Something is severely wrong here. Is that the exact code that you are using? Coz a couple of rogue commas suggest otherwise. – Matt Kocaj Nov 15 '09 at 11:02
  • cottsak, if you're referring to the comma after f.FruitName, apparently that is valid syntax. I saw this in T4MVC's settings files. Sincethe compiler seems to ignore the extra trailing comma, I find convenient to just leave it there. This way all lines follow the same format, and I don't have to remember to always remove last comma. I tried removing it just to make sure and, sure enough, it made no difference. – Daniel Liuzzi Nov 15 '09 at 11:15
  • This is so bizarre! I can't believe I've reproduced this. – Matt Kocaj Nov 15 '09 at 16:19

2 Answers2

1

There is no real answer to this. I have encountered this more often then not. keeping the order of assignment same as the order of reading help. ie. in Debug time if you see the QueryText and see in what order the columns are read from db. and then keep the assignment same as the order of sql Select Statement in QueryText. Resolves the problem 9 out of 10 times.

Anuj Pandey
  • 938
  • 2
  • 11
  • 30
0

Try this instead:

IList<FruitView> fruits = (
         from f in db.Fruits
             select new FruitView {
                 MyFruitID = f.FruitID,
                 MyFruitName = f.FruitName
             }).ToList();

or

IQueryable<FruitView> fruits = 
         from f in db.Fruits
             select new FruitView {
                 MyFruitID = f.FruitID,
                 MyFruitName = f.FruitName
             };

..and foreach as before.

Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79