2017-04-26 62 views
0

我不确定这种事情是否可能。但我想使用一个变量作为列名。如何在asp.net中使用字符串变量作为列名mvc

下面是我必须用

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(); 

代码,但我想用它象下面。

我想使用的代码:

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(); 

我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; } 
} 

我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; } 
} 

我有1到1 stock.cs关系和变异.sd

回答

0

这应该工作:

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

这不起作用。因为“ColorName”不是列。我将颜色定义为列。 – senjizu

+0

然后我想我需要更多的代码来看看你的数据类型是如何布局的。 –

+0

我更新了我的问题 – senjizu

0

我创建了一个Method将采取listColumn Name然后返回列value。请检查这个。

代码:

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; 
    } 
} 

你必须使用的方法GetColumnValue在你的代码是这样的:

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(); 

可以在DotNetFiddle运行此解决方案。

+0

imho,你错过了标签'entity-framework'。你的代码运行良好的linq对象,但不适用于实体。 – tschmit007

+0

谢谢你的回答。它工作正常,但我会改变表的结构,它会更有用。 – senjizu

相关问题