C# 要使用LINQ列出(和返回)的列
我有一个包含列Col1、Col2、Col3等的SQL记录表。。。都是同一类型,都是同一事物 表和这些列之间的关系本质上是一对多的关系。我可以创建另一个表,并给出它们之间的关系,但我有许多不相关的表,都有Col1、Col2、Col3等。。。这需要我多次复制表 我需要一种方法将这些列转换为列表,并将列表元素放回列中。数据的顺序并不重要。如果Col1包含A,Col2包含B,则更新后Col1包含B,Col2包含A是可以的 我画了一幅漂亮的画作插图 这是一种非常丑陋的方法,可以将数据放回内存C# 要使用LINQ列出(和返回)的列,c#,wpf,entity-framework-5,C#,Wpf,Entity Framework 5,我有一个包含列Col1、Col2、Col3等的SQL记录表。。。都是同一类型,都是同一事物 表和这些列之间的关系本质上是一对多的关系。我可以创建另一个表,并给出它们之间的关系,但我有许多不相关的表,都有Col1、Col2、Col3等。。。这需要我多次复制表 我需要一种方法将这些列转换为列表,并将列表元素放回列中。数据的顺序并不重要。如果Col1包含A,Col2包含B,则更新后Col1包含B,Col2包含A是可以的 我画了一幅漂亮的画作插图 这是一种非常丑陋的方法,可以将数据放回内存 int fi
int fieldCount = 0;
foreach (string field in MyList)
{
switch (fieldCount++)
{
case 1:
entityObject.Col1 = field;
break;
case 2:
entityObject.Col2 = field;
break;
case 3:
entityObject.Col3 = field;
break;
case 4:
entityObject.Col4 = field;
break;
case 5:
entityObject.Col5 = field;
break;
}
}
您可以为此使用反射:
//replace object parameter with some base class if possible
public static List<string> GetColumnValues(object item)
{
return item.GetType().GetProperties()
.Where(prop => prop.Name.StartsWith("Col")
&& prop.PropertyType == typeof(string))
.Select(prop => prop.GetValue(item) as string)
.ToList();
}
然后,要设置它们,可以再次使用反射:
//replace object parameter with some base class if possible
public static void GetColumnValues(object item, List<string> values)
{
foreach(var pair in item.GetType().GetProperties()
.Where(prop => prop.Name.StartsWith("Col")
&& prop.PropertyType == typeof(string))
.Zip(values, (prop, value) => new{prop,value})
{
pair.prop.SetValue(item, pair.value);
}
}
您可以为此使用反射:
//replace object parameter with some base class if possible
public static List<string> GetColumnValues(object item)
{
return item.GetType().GetProperties()
.Where(prop => prop.Name.StartsWith("Col")
&& prop.PropertyType == typeof(string))
.Select(prop => prop.GetValue(item) as string)
.ToList();
}
然后,要设置它们,可以再次使用反射:
//replace object parameter with some base class if possible
public static void GetColumnValues(object item, List<string> values)
{
foreach(var pair in item.GetType().GetProperties()
.Where(prop => prop.Name.StartsWith("Col")
&& prop.PropertyType == typeof(string))
.Zip(values, (prop, value) => new{prop,value})
{
pair.prop.SetValue(item, pair.value);
}
}
如果速度很重要,那么最好避免使用反射。一种更简洁的方法是设置一组setter,然后循环:
var setters = new Action<string>[]
{
val => entityObject.Col1 = val,
val => entityObject.Col2 = val,
val => entityObject.Col3 = val,
val => entityObject.Col4 = val,
val => entityObject.Col5 = val,
};
for (int i=0 ; i<MyList.Count ; i++)
setters[i](MyList[i]);
如果您能够承受性能的打击,那么反射将产生更少的代码行/更少的重复:
Type type = entityObject.GetType();
PropertyInfo[] props = Enumerable.Range(1, MyList.Count).Select(num => type.GetProperty("Col" + num)).ToArray();
for (int i=0 ; i<MyList.Count ; i++)
props[i].SetValue(entityObject, MyList[i]);
当然,也有比反射更快的替代方法,例如。如果速度很重要,那么最好避免使用反射。一种更简洁的方法是设置一组setter,然后循环:
var setters = new Action<string>[]
{
val => entityObject.Col1 = val,
val => entityObject.Col2 = val,
val => entityObject.Col3 = val,
val => entityObject.Col4 = val,
val => entityObject.Col5 = val,
};
for (int i=0 ; i<MyList.Count ; i++)
setters[i](MyList[i]);
如果您能够承受性能的打击,那么反射将产生更少的代码行/更少的重复:
Type type = entityObject.GetType();
PropertyInfo[] props = Enumerable.Range(1, MyList.Count).Select(num => type.GetProperty("Col" + num)).ToArray();
for (int i=0 ; i<MyList.Count ; i++)
props[i].SetValue(entityObject, MyList[i]);
当然,也有比反射更快的选择,比如