在SharePoint 2010中使用Linq时候,请注意特殊字符
2011-03-20 10:28
399 查看
SharePoint 2010 增加了对LINQ的支持,增强了开发的能力。那么其中好事有一些晓得细节需要我们注意的。
例子:有一个自定义列表“Citys”, 其中包含一个”选项”类型的字段“GDP”,它包含如下的两个选项:
那么你有没有尝试过如何去使用LINQ获得这列的值,我们来看一下:
首先使用SPMetal工具生成实体类,我们在实体类中找到“GDP”这个字段:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
如果这个时候,你直接使用
来获得这列的值,那么你得到的肯定是“_5PerUserMonth” 或者 “_10PerCompanyMonth” , 而没有办法得到他真正的值。谁叫C#的变量命名规则不让使用“空格”或者“/”等呢。这个时候我们需要利用发射来获取到里面的值。
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
例子:有一个自定义列表“Citys”, 其中包含一个”选项”类型的字段“GDP”,它包含如下的两个选项:
那么你有没有尝试过如何去使用LINQ获得这列的值,我们来看一下:
首先使用SPMetal工具生成实体类,我们在实体类中找到“GDP”这个字段:
internal enum GDP : int { None = 0, Invalid = 1, [Microsoft.SharePoint.Linq.ChoiceAttribute(Value = "5 per user/month")] _5PerUserMonth = 2, [Microsoft.SharePoint.Linq.ChoiceAttribute(Value = "10 per company/month")] _10PerCompanyMonth = 4, }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
如果这个时候,你直接使用
item.GDP.Value
来获得这列的值,那么你得到的肯定是“_5PerUserMonth” 或者 “_10PerCompanyMonth” , 而没有办法得到他真正的值。谁叫C#的变量命名规则不让使用“空格”或者“/”等呢。这个时候我们需要利用发射来获取到里面的值。
public void GetGDP() { var ctx = new CityDataContext(WebUrl); var cityList = from c in ctx.Citys select c; foreach (var item in cityList) { Console.WriteLine(stringValueOf(item.GDP)); } } public string stringValueOf(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); Microsoft.SharePoint.Linq.ChoiceAttribute[] attributes = (Microsoft.SharePoint.Linq.ChoiceAttribute[])fi.GetCustomAttributes( typeof(Microsoft.SharePoint.Linq.ChoiceAttribute),false); if (attributes.Length > 0) { return attributes[0].Value; } else { return value.ToString(); } }
如果列表中的其他列里面也存有这样的值,那么你使用的时候还真的要注意了。
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- SharePoint 2010如何才能使用LINQ的功能
- 'Invalid parameter not satisfying: URLString'网络请求的时候get使用特殊字符和汉字崩溃的恶心bug解决方法
- 'Invalid parameter not satisfying: URLString'网络请求的时候get使用特殊字符和汉字崩溃的恶心bug解决方法
- SharePoint 2010中使用LINQ To SharePint
- js:使用JSON.stringify的时候,对\n等特殊字符的处理
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- SharePoint 2010 Reporting Services 报表编译错误 项“”的路径无效完整路径的长度必须小于260个字符 解决方案
- SharePoint 2010中使用Visual Studio 2010进行方便快速的Web Part开发
- 使用DataTable.Select 方法时,特殊字符的转义方法分享
- 正则表达式中的字符含义和特殊使用
- 使用Visual Studio 2010与Visual Studio 11“合作”开发SharePoint 2011
- Sharepoint栏位包含特殊字符
- HTML字符实体(CharacterEntities),转义字符串(Escape Sequence),web.config中使用特殊字符
- [转]SharePoint 2010/2013 使用Javascript来判断权限的三种方法
- SharePoint 2010 使用url参数过滤列表视图数据(使用get方法过滤列表数据)
- [SharePoint 2010] 使用Designer 2010修改List item的链接地址
- LINQ当集合为空的时候,如何使用Max,Min,Sum等聚合函数?
- 获取特殊字符后面所有字符(适用测试时只带一个参数的时候)
- 使用AJAX传XML数据的时候,需要将某些XML字符转义