Let's say I have:
List<string> sqlServerNames;
and a method:
List<string> GetDatabases( string serverName );
And I want a single query to get a list of { serverName, databaseName }
Is there a way to do this with Linq?
sqlServerNames.????
So for example, if I have two servers, ".", and "SQL2016".
And "." has "db1", "db2"
And "SQL2016" has "db3"
I'd want:
".", "db1"
".", "db2"
"SQL2016", "db3"
Probably with an anonymous type.
答案 0 :(得分:3)
尝试看起来像这样的东西:
var dictionary = {
coolSound: {
sound: function (mode) {
return new Howl({src: ["sounds/" + mode + "/bubbles.mp3"]});
}
}
}
答案 1 :(得分:3)
建议使用SelectMany
的另一个重载。其他答案需要为每个服务器名称分配一个新的枚举/枚举/委托,而这并不是。
sqlServerNames.SelectMany(serverName => GetDatabases(serverName),
(serverName, databaseName) => new
{
ServerName = serverName,
DatabaseName = databaseName
});
相当于:
from serverName in sqlServerNames
from databaseName in GetDatabases(serverName)
select new
{
ServerName = serverName,
DatabaseName = databaseName
};
答案 2 :(得分:2)
您可以使用SelectMany
方法:
sqlServerNames
.SelectMany(sn => GetDatabases(sn)
.Select(dbn => new {
ServerName = sn,
DatabaseName = dbn
}
)
);