1

就像标题暗示我的问题是我有一个查询/存储过程从视图中选择数据并且它在管理工作室工作得很好,问题是当我尝试从我的应用程序调用这些数据时(使用 linq 来实体)我得到错误的数据(当查询应返回 5 个不同的行/记录时,单行重复 10 次错误)这是我的管理工作室查询:

select * from dbo.v_RouteCardDetails_SizeInfo
where  Trans_TransactionHeader = 0
AND Direction = 0
AND RoutGroupID = 1
AND Degree = '1st'

返回结果:

Size    SizeQuantity    Trans_TransactionHeader RoutGroupID Direction   Degree

XS  10  0   1   0   1st
S   2   0   1   0   1st
M   0   0   1   0   1st
L   5   0   1   0   1st
XXL 2   0   1   0   1st

这是我的 Linq 查询:

    (from x in context.v_RouteCardDetails_SizeInfo
                            where  x.Trans_TransactionHeader == 0
                                && x.Direction == 0
                                && x.RoutGroupID == 1
                                && x.Degree.ToLower() == "1st"
                        select x).ToList<_Model.v_RouteCardDetails_SizeInfo>();

返回的结果是:

Size    SizeQuantity    Trans_TransactionHeader RoutGroupID Direction   Degree

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

XS  10  0   1   0   1st

2天我一直在尝试解决这个问题,感谢您的帮助谢谢

4

2 回答 2

4

毫无疑问,Entity Framework 猜测为视图主键的字段在视图中并不是唯一的。尝试在 edmx 设计器(或代码优先映射)中将字段添加到 PK 中,直到您真正获得独特的组合。

EF 只是为它在 SQL 查询的结果集中找到的每个相同的键值具体化相同的行。

于 2013-06-19T10:13:13.190 回答
1

Because it is not possible to have sme enviroment as your I suggest you do following things:

  1. Check in debbuger what exactly is in list. The printed result suggest that you somehow display data returned form database and in this code could be an error.
  2. Preview LINQ query. You can use LinqPad for this.
于 2013-06-19T10:11:57.910 回答