简体   繁体   English

LINQ选择子句类型混淆

[英]LINQ select clause type confusion

I have the following model 我有以下型号

namespace Models
{
  [System.Data.Linq.Mapping.Table(Name = "RegionTable")]
  public class Region
  {
    [System.Data.Linq.Mapping.Column]
    public int ID { get; set; }

    [System.Data.Linq.Mapping.Column]
    public string Name { get; set; }

    [System.Data.Linq.Mapping.Column]
    public string OrchardRoleID { get; set; }
  }
}

and i want to select all regions and return it via a list of Region model objects. 我想选择所有区域并通过Region模型对象列表将其返回。

Why is this working 为什么这样工作

var lQuery = from r in lDBContext.Regions new Models.Region { ID = r.ID, Name = r.Name };

var l = lQuery.ToList();

but the following is not 但是以下不是

var lQuery = from r in lDBContext.Regions select r;

var l = lQuery.ToList();

as it throws a 当它抛出一个

 Unable to cast object of type 'System.Int32' to type 'System.String'.

exception? 例外?

Your issue is because 你的问题是因为

public string OrchardRoleID { get; set; }

i suppose that this is a int in your database in your fist example it works because you're omitting that field 我想这在您的第一个示例中是数据库中的int ,它可以工作,因为您省略了该字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM