如何从不同的表中分配变量?

时间:2016-04-20 16:01:15

标签: c# sql linq visual-studio visual-studio-2012

我有一份Make和Make of Assets表的列表。我需要使用AssetMakeString分配Asset Make。目前我有:

    public class FuelLightTruckDataSource
    {
        public Model.Asset Asset { get; set; }
        public Employee Employee { get; set; }
        public FuelProductClass ProductClass { get; set; }
        public FuelVendor Vendor { get; set; }
        public FuelFillSource FillSource { get; set; }
        public AssetModel Model { get; set; }
        public AssetMake Make { get; set; }
        public string Source { get; set; }
        public string City { get; set; }
        public decimal Volume { get; set; }
        public decimal? Distance { get; set; }
        public decimal? Odomoter { get; set; }
        public decimal? DistanceVolume { get; set; }
        public float? ConsumptionRate { get; set; }
        public DateTime? Date { get; set; }
        public decimal? ConsumptionCalculated { get; set; }
        public decimal? DistanceCalculated { get; set; }

    public FuelLightTruckDataSource() { }

    public static List<FuelLightTruckDataSource> Get(DateTime from, DateTime to)
    {
        List<FuelLightTruckDataSource> data = new List<FuelLightTruckDataSource>();

        using (SystemContext ctx = new SystemContext())
        {

            List<FuelLightTruckDataSource> dataTransactions
                = ctx.FuelTransaction
                    .Where(tx => DbFunctions.TruncateTime(tx.DateTime) >= from.Date && DbFunctions.TruncateTime(tx.DateTime) <= to.Date
                        //&& tx.AssetFilled.AssignedToEmployee.Manager
                        && tx.AssetFilled.AssignedToEmployee != null
                        //&
                        && tx.AssetFilled.AssetType.Code == "L"
                        && (tx.FuelProductType.FuelProductClass.Code == "GAS" || tx.FuelProductType.FuelProductClass.Code == "DSL"))
                    .GroupBy(tx => new { tx.AssetFilled, tx.DateTime, tx.FuelProductType.FuelProductClass, tx.FuelCard.FuelVendor, tx.City, tx.Volume, tx.Odometer}) //Added tx.volume to have individual transactions
                    .Select(g => new FuelLightTruckDataSource()
                    {
                        Asset = g.FirstOrDefault().AssetFilled,
                        Model = g.FirstOrDefault().AssetFilled.AssetModel,
                        Make = g.FirstOrDefault().AssetFilled.AssetModel.AssetMakeString,
                        Employee = g.FirstOrDefault().AssetFilled.AssignedToEmployee,
                        ProductClass = g.FirstOrDefault().FuelProductType.FuelProductClass,
                        Vendor = g.FirstOrDefault().FuelCard.FuelVendor,
                        FillSource = FuelFillSource.Transaction,
                        Source = "Fuel Station",
                        City = g.FirstOrDefault().City.ToUpper(),
                        Volume = g.FirstOrDefault().Volume,
                        Distance = g.FirstOrDefault().Odometer,
                        Date = g.FirstOrDefault().DateTime


                    })
                   // .OrderBy(tx => tx.Date)
                    .ToList();

我的AssetModel类如下所示:

{
    public class AssetModel
    {
        public int Id { get; set; }
        [Required]
        [MaxLength(100)]
        [Index("IX_AssetModel", 2, IsUnique = true)]
        public string Name { get; set; }
        [MaxLength(10)]
        [Index("IX_AssetModel", 3, IsUnique = true)]
        public string Year { get; set; }
        public string Description { get; set; }

        [Index("IX_AssetModel", 1, IsUnique = true)]
        public int? AssetMakeId { get; set; }

        public virtual AssetMake AssetMake { get; set; }
        public virtual List<Asset> Assets { get; set; }

    // **** Custom Properties ****

    public string AssetMakeName { get { return AssetMake.Name; } }
    public string AssetMakeString { get { return AssetMake.Name + " " + Name + (Year == null ? "" : " (" + Year + ")"); } }
}

}

为什么我会收到“错误:无法将类型'string'隐式转换为'PSI.ManagementSystem.Model.AssetMake'”?

1 个答案:

答案 0 :(得分:0)

FuelLightTruckDataSource.Make声明如下:

public AssetMake Make { get; set; }

Make的类型为AssetMake。您无法为其指定字符串。您必须为其分配AssetMake

相反,你在这里为它分配一个字符串,编译器不会接受它,因为它无法将字符串隐式转换为AssetMake

Make = g.FirstOrDefault().AssetFilled.AssetModel.AssetMakeString

AssetMakeString是一个字符串。它的声明如下:

public string AssetMakeString { get { 
    return AssetMake.Name + " " + Name 
           + (Year == null ? "" : " (" + Year + ")"); } }

因此,您无法使用该字符串初始化FuelLightTruckDataSource.Make。而不是让资产让您从数据库中获取,将其转换为字符串,然后将其分配给您无法分配的内容,您要做的只是分配资产make本身。

您认为您可以找到属于AssetModel的{​​{1}}属性吗?如果您阅读AssetMake的类定义,您可能会找到一个。如果您可以找到一个,那么可能是您要使用的属性而不是AssetModel