2

所以我有一个办公地点表,每一列都包含相关信息(即办公室名称、地址、城市、州、邮编、国家)。

我在界面中设置了供用户选择办公室,其余的都是为他们填写的。

我知道如何使用通用列表并使用包含多行的一列填充它,就像我使用从同一张表中列出办公室位置的下拉菜单一样。

我不知道如何用单行中的多列填充通用列表。

例如,这是我为前者工作的代码:

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

不知道为什么以前没有人问过这个问题。

4

3 回答 3

4

你的第一种方法可以简化:

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

还要尽量避免使用匈牙利表示法(为变量名添加前缀,用于描述变量类型)。而不是strLoc你可以使用locationName.

于 2012-07-12T13:28:42.343 回答
3

你的第一种方法可能是

return new Locs().Locations.OrderBy(m => m.Office).Select(l => l.OFFICE).ToList()

您的第二个查询可能不应该返回一个列表,而是一个位置

private Location selectAddr(string strLoc)
{
    return new Locs().Locations.FirstOrDefault(m => m.OFFICE == strlLoc);
}

或者如果你想要一个地址列表(这不是很清楚)

private IList<string> selectAddr(string strLoc)
{
    return new Locs().Locations.Where(m => m.OFFICE == strlLoc)
    .Select(l => l.ADDRESS).ToList();
}

或位置列表

private IList<Location> selectAddr(string strLoc)
{
    return new Locs().Locations.Where(m => m.OFFICE == strlLoc)
    .ToList();
}
于 2012-07-12T13:30:49.303 回答
2

塞弗瑟斯。以上所有答案都是有效的,但我不确定他们是否回答了您的问题。您在问如何从一行返回多列,对吗?

您可以在这里使用匿名类型来提供帮助,例如

private IList<string> selectAddr(string strLoc)
{
    Locs offLocs = new Locs();
    var xx = (from l in offLocs.Locations
            where l.OFFICE == strLoc
            select new { l.Address1, l.Address2, l.County });

return xx.Select(a=>String.Concat(x.Address1, x.Address2, x.County).ToList();

}

但是,您可以进一步简化它,

private IList<string> selectAddr(string strLoc)
    {
        Locs offLocs = new Locs();
        return (from l in offLocs.Locations
                where l.OFFICE == strLoc
                select String.Concat(l.Address1, l.Address2, l.County)).ToList();

    }

或者更好

var offLocs = new Locs();
return offLocs.Where(o=>o.Office = strLoc).Select(l=>String.Concat(l.Address1, l.Address2, l.County)).ToList();

但这只是口味问题!

于 2012-07-12T13:43:46.427 回答