SubSonic 3 Linq 投影匿名类型但不投影类类型

发布于 2024-08-11 04:02:58 字数 1644 浏览 8 评论 0原文

我对 SubSonic 3/Linq 相当陌生,不知道我是否遗漏了一些明显的东西,但我认为我遇到了投影问题。我正在尝试执行最基本的查询,并且仅在使用匿名类型时才返回正确的结果。当我将匿名类型与常规类类型交换时,我将所有属性设置为空/零。我正在制作一个 DAL 类库,因此不幸的是匿名类型不是一个选项。

片段

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

我的问题有点类似于 这个,只是我什至没有进行连接;只是简单的选择。

任何线索将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

絕版丫頭 2024-08-18 04:02:58

对此没有真正的答案。我遇到过这种情况的次数比没有遇到过。保持作业顺序与阅读顺序相同有帮助。 IE。在调试时,如果您看到 QueryText 并查看从数据库读取列的顺序。然后保持与QueryText中sql Select语句的顺序相同的赋值。十次中有九次解决问题。

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.

凉城已无爱 2024-08-18 04:02:58

试试这个:

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

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

..and foreach 像以前一样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文