English 中文(简体)
SubSonic 3 Linq projecting anonymous types but not class types
原标题:

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 + "	" + 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.

问题回答

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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签