LINQ查询来获取在Silverlight列标题 [英] LINQ Query to get Column Headers in Silverlight
问题描述
我正在使用WCF服务,我需要从一个特定的表中的所有列标题Silverlight应用程序。我一直在试图写一个LINQ查询要做到这一点,但到目前为止,我还没有能够得到它的正常工作。我还没有找到有关这一非常多的信息。我发现下面的信息,但我有困难,连接到我的数据。
的http://www.c-sharpcorner.com/UploadFile/dhananjay$c$cr/4856/#ReadAndPostComment
到目前为止,我已经尝试了以下......这不会因DataContext的编制需要一个参数,这是我在哪里卡住了。
公开名单<字符串> GetColumnHeaders()
{
DataContext的背景下=新的DataContext();
名单<字符串> columnList =新的名单,其中,串>();
VAR数据模型= context.Mapping;
的foreach(在dataModel.GetTables变种R())
{
如果(r.TableName.Equals(表1,StringComparison.InvariantCultureIgnoreCase))
{
的foreach(在r.RowType.DataMembers变种C)
{
columnList.Add(c.MappedName);
}
}
}
返回columnList;
}
而不是使用的DataContext上下文=新的DataContext()的;
我尝试以下,但我知道这个问题是一样的。
VAR数据模型=新AttributeMappingSource()
.GetModel(
typeof运算(RepositoryBase< HBS_SondesEntities>
));
下面是一个解决方案,我的最好的尝试,它很难真正理解你已经尝试/写。
公开名单<字符串> GetColumnHeaders(){
名单<字符串> columnList =新的名单,其中,串>();
使用(SondesEntities上下文=新HBS_SondesEntities()){
的foreach(在context.Mapping.GetTables变种R()){
如果(r.TableName
.Equals(表1,StringComparison.InvariantCultureIgnoreCase)){
的foreach(在r.RowType.DataMembers变种C){
columnList.Add(c.MappedName);
}
}
}
}
返回columnList;
}
假如我没胖手指的东西在这里是使用LINQ相同code。
公开名单<字符串> GetColumnHeaders(){
名单<字符串> columnList =新的名单,其中,串>();
使用(SondesEntities上下文=新HBS_SondesEntities()){
VAR的查询=(
context.Mapping.GetTables()
。凡(T => t.TableName
.Equals(
表格1,
StringComparison.InvariantCultureIgnoreCase)
)
).SelectMany(X => x.RowType.DataMembers);
columnList = query.Select(米=> m.MappedName).ToList()
}
返回columnList;
}
I am working on a Silverlight application using a WCF service where I need to get all the Column Headers from a specific table. I have been trying to write a LINQ query to do this, but so far I have not been able to get it to work correctly. I have not found very much information pertaining to this. I have found the following information, but I have had difficulties connecting to my data.
http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/4856/#ReadAndPostComment
So far I have tried the following...This will not compile due to DataContext needing a parameter and that is where I am stuck.
public List<string> GetColumnHeaders()
{
DataContext context = new DataContext();
List<string> columnList = new List<string>();
var dataModel = context.Mapping;
foreach (var r in dataModel.GetTables())
{
if (r.TableName.Equals("table1", StringComparison.InvariantCultureIgnoreCase))
{
foreach (var c in r.RowType.DataMembers)
{
columnList.Add(c.MappedName);
}
}
}
return columnList;
}
Instead of using DataContext context = new DataContext();
I tried the following, but I know the problem is the same.
var dataModel = new AttributeMappingSource()
.GetModel(
typeof(RepositoryBase<HBS_SondesEntities>
));
Here is my best attempt at a solution, its hard to really understand what you have tried/written.
public List<string> GetColumnHeaders(){
List<string> columnList = new List<string>();
using (SondesEntities context = new HBS_SondesEntities()){
foreach (var r in context.Mapping.GetTables()){
if (r.TableName
.Equals("table1", StringComparison.InvariantCultureIgnoreCase)) {
foreach (var c in r.RowType.DataMembers){
columnList.Add(c.MappedName);
}
}
}
}
return columnList;
}
Assuming I didn't fat finger something here is the same code using linq.
public List<string> GetColumnHeaders(){
List<string> columnList = new List<string>();
using (SondesEntities context = new HBS_SondesEntities()){
var query = (
context.Mapping.GetTables()
.Where(t=>t.TableName
.Equals(
"table1",
StringComparison.InvariantCultureIgnoreCase)
)
).SelectMany(x=>x.RowType.DataMembers);
columnList = query.Select(m=>m.MappedName).ToList()
}
return columnList;
}
这篇关于LINQ查询来获取在Silverlight列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!