所以我有一张办公地点表,每一列都包含相关信息(即办公室名称、地址、城市、州、邮政编码、国家/地区)。
我已经在界面中设置好让用户选择他们的办公室,剩下的由他们填写。
我知道如何使用通用列表并用一列多行填充它,就像我使用列出同一张表中办公地点的下拉菜单所做的那样。
我不知道如何用单行中的多列填充通用列表。
例如,这是我为前者准备的代码,它运行良好:
private List<string> selectLocs()
{
Locs offLoc = new Locs();
List<string> objList = new List<string>();
var select = from l in offLoc.Locations
orderby l.OFFICE
select l;
foreach (Location loc in select)
{
objList.Add(loc.OFFICE);
}
return objList;
}
这是后一种情况的代码,不完整,因为我不知道如何做同样的事情:
private List<string> selectAddr(string strLoc)
{
List<string> objList = new List<string>();
Locs offLocs = new Locs();
var select = from l in offLocs.Locations
where l.OFFICE == strLoc
select l;
//foreach what? in select? Do I even use a foreach loop?
return objList;
}
不知道为什么以前没有人问过这个问题。
最佳答案
第一种方法可以简化:
private List<string> SelectLocations()
{
Locs offLoc = new Locs();
return (from l in offLoc.Locations
orderby l.OFFICE
select l.OFFICE).ToList();
}
第二种方法:
private List<string> SelectAddresses(string strLoc)
{
Locs offLocs = new Locs();
return (from l in offLocs.Locations
where l.OFFICE == strLoc
select l.ADDRESS).ToList();
}
更新:如果您需要多个属性,则返回完整的位置对象:
private List<Location> SelectLocations(string strLoc)
{
Locs offLocs = new Locs();
return (from l in offLocs.Locations
where l.OFFICE == strLoc
select l).ToList();
}
同时尽量避免使用匈牙利表示法(给变量名添加前缀,描述变量类型)。您可以使用 locationName
而不是 strLoc
。
关于c# - 如何使用 LINQ to SQL 将单行添加到列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452778/