SubSonic 3 Linq 投影匿名类型但不投影类类型
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对此没有真正的答案。我遇到过这种情况的次数比没有遇到过。保持作业顺序与阅读顺序相同有帮助。 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.
试试这个:
或
..and
foreach
像以前一样。Try this instead:
or
..and
foreach
as before.