简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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