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

本文关键字:分配 变量 | 更新日期: 2024-10-31 07:05:30

我有一个资产的品牌和型号表列表。我需要使用 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();

我的资产模型类如下所示:

{
    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 + ")"); } }
}

}

为什么我收到"错误:无法将类型'字符串'隐式转换为'PSI。管理系统.模型.资产制造'"?

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

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,将其转换为字符串,然后将其分配给您无法分配的内容,不如直接分配资产 make 本身。

你认为你能找到AssetModel AssetMake类型的属性吗?如果您阅读 AssetModel 的类定义,您可能会找到一个。如果可以找到一个,则可能是您要使用的属性,而不是AssetMakeString