亚音速3 LINQ的投影匿名类型而不是类类型 [英] SubSonic 3 Linq projecting anonymous types but not class types

查看:245
本文介绍了亚音速3 LINQ的投影匿名类型而不是类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的亚音速3 / LINQ的,不知道如果我失去了一些东西很明显,但我觉得我遇到了一个投影的问题。我试图完成最基本的查询,并取回只有当我使用匿名类型正确的结果。那一刻我换匿名类型与常规类的类型,我得到的设置为null /零的所有属性。我制作一个DAL类库,所以很遗憾匿名类型是不是一种选择。

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.

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; }
    }

}



所以,这并未科技工作(返回所有空/零)

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



可正常工作

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.

任何线索将非常感激。

推荐答案

有没有真正的答案。我也更经常遇到这样的话不行。保持分配相同的阅读帮助订单的订单。 IE浏览器。在调试的时间,如果你看到QUERYTEXT,看以什么顺序列从数据库读取。然后保持分配相同QUERYTEXT SQL SELECT语句的顺序。解决问题9个10倍。

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.

这篇关于亚音速3 LINQ的投影匿名类型而不是类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆