简体   繁体   中英

how to use string variable as column name in asp.net mvc

I am not sure that such a thing is possible. But I want to use a variable as column name.

Below is the code I have to use

cartall.CartItems = cartdatas.Select(a => new Models.DTO.CartDTO.CartVM()
{
    VariationId = a.VariationId,
    ColorName = a.ColorName,
    StockInfo = rpstock.FirstOrDefault(x => x.Id == a.VariationId).Yellow
}).ToList();

But I want to use it like below.

The code I want to use:

cartall.CartItems = cartdatas.Select(a => new Models.DTO.CartDTO.CartVM()
{
    VariationId = a.VariationId,
    ColorName = a.ColorName,
    StockInfo = rpstock.FirstOrDefault(x => x.Id == a.VariationId).(a.ColorName)
}).ToList();

My Stock.cs

public class Stock:Base.BaseEntity
{
    public int Id { get; set; }
    public int? Yellow { get; set; }
    public int? Red { get; set; }
    public int? White { get; set; }

    public virtual VariationEntity.Variation Variation { get; set; }
}

My Variation.cs

public class Variation : Base.BaseEntity
{
    public int Id { get; set; }
    public int OwnerProductID { get; set; }
    public string SKU { get; set; }
    public short? Height { get; set; }
    public short? Width { get; set; }
    public decimal? Price { get; set; }
    public string Delivery { get; set; }
    public int? OrderLimit { get; set; }

    public virtual StockEntity.Stock Stock { get; set; }
}

I have 1 to 1 relationship between stock.cs and variation.cs

这应该工作:

 StockInfo = rpstock.FirstOrDefault(x => x.Id == a.VariationId && x.ColorName == a.ColorName)

I've created a Method which will take a list and a Column Name then return value of the column. Please check this.

Code:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public class Stock
    {
        public int Id { get; set; }
        public int? Yellow { get; set; }
        public int? Red { get; set; }
        public int? White { get; set; }
    }

    public static void Main()
    {
        List<Stock> list = new List<Stock>();
        list.Add(new Stock(){Id=1, Yellow = 50, Red = 0, White = 205});
        list.Add(new Stock(){Id=2, Yellow = 20, Red = 200, White = 35});
        list.Add(new Stock(){Id=3, Yellow = 0, Red = 100, White = 155});

        string ColumnName = "Yellow";

        var Test1 = GetColumnValue(list.Where(m=>m.Id == 1).ToList(), ColumnName);
        var Test2 = GetColumnValue(list.Where(m=>m.Id == 2).ToList(), ColumnName);
        var Test3 = GetColumnValue(list.Where(m=>m.Id == 3).ToList(), ColumnName);

        Console.WriteLine(Test1);
        Console.WriteLine(Test2);
        Console.WriteLine(Test3);
    }

    public static object GetColumnValue(List<Stock> items, string columnName)
    {
        var values = items.Select(x => x.GetType().GetProperty(columnName).GetValue(x)).FirstOrDefault();
        return values;
    }
}

You have to use the method GetColumnValue in your code like this:

cartall.CartItems = cartdatas.Select(a => new Models.DTO.CartDTO.CartVM()
{
    VariationId = a.VariationId,
    ColorName = a.ColorName,
    StockInfo = GetColumnValue(rpstock.FirstOrDefault(x => x.Id == a.VariationId).ToList(), a.ColorName)
}).ToList();

You can run this solution in DotNetFiddle .

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