如何将行号投影到linq查询结果集上.
而不是说:
field1,field2,field3
field1,field2,field3
我想要:
1,field1,field2,field3
2,field1,field2,field3
以下是我对此的尝试:
public List<ScoreWithRank> GetHighScoresWithRank(string gameId, int count)
{
Guid guid = new Guid(gameId);
using (PPGEntities entities = new PPGEntities())
{
int i = 1;
var query = from s in entities.Scores
where s.Game.Id == guid
orderby s.PlayerScore descending
select new ScoreWithRank()
{
Rank=i++,
PlayerName = s.PlayerName,
PlayerScore = s.PlayerScore
};
return query.ToList<ScoreWithRank>();
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,"Rank = i ++"行引发了以下编译时异常:
"表达式树可能不包含赋值运算符"
我正在尝试阅读用户的公共推特状态,以便我可以在我的Windows Phone应用程序中显示它.
我正在使用Scott Gu的例子:http: //weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
当我的代码从异步调用返回时,一旦我尝试使用e.Result,我就会收到"System.Security.SecurityException".
我知道我的uri是正确的,因为我可以在浏览器中填充它并获得良好的结果.
这是我的相关代码:
public void LoadNewsLine()
{
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=krashlander"));
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XElement xmlTweets = XElement.Parse(e.Result); //exception thrown here!
var message = from tweet in xmlTweets.Descendants("status")
select tweet.Element("text").Value;
//Set message and tell UI to update.
//NewsLine = message.ToString();
//RaisePropertyChanged("NewsLine");
}
Run Code Online (Sandbox Code Playgroud)
任何人的想法?
解决方案:我终于想出了这个.我只是忘了取消注释WMAppManifest.xml中的:功能.一旦我这样做,安全例外就消失了.
在Silverlight 4项目中,我有一个扩展Canvas的类:
public class AppendageCanvas : Canvas
{
public float Friction { get; set; }
public float Restitution { get; set; }
public float Density { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在Blend中使用此画布,将其拖动到另一个控件并设置自定义属性:
alt text http://www.farseergames.com/storage/share/PropertyInBlend.Png
当我运行应用程序时,在包含我的自定义画布的控件上调用InitializeComponent时出现以下错误:
Failed to create a 'Windows.Foundation.Single' from the text '0.0'
Run Code Online (Sandbox Code Playgroud)
alt text http://www.farseergames.com/storage/share/SilverlightError.Png
我不确定为什么Silverlight无法将此属性从它在Xaml中的字符串表示转换为浮点数.
有人有主意吗?
我想知道是否有更好的方法(比我目前正在做的)只使用对象和属性字符串名来获取并保持对另一个对象中的属性的引用.特别是,使用.Net 4.0的新动态功能有更好的方法吗?
这就是我现在所拥有的.
我有一个" PropertyReference<T>"对象,它在构造函数中获取对象名称和属性名称.
一种Initialize()方法使用反射来找到对象和属性并且存储属性getter作为Action<T>和属性setter作为Func<T>.
当我想要实际调用属性时,我会做这样的事情:
int x = _propertyReference.Get();
Run Code Online (Sandbox Code Playgroud)
要么
_propertyReference.Set(2);
Run Code Online (Sandbox Code Playgroud)
这是我的PropertyReference<T>代码.请剖析并提出改进建议.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml;
namespace WindowsFormsApplication2
{
public class PropertyReference<T> : IPropertyReference
{
public string ComponentName { get; set; }
public string PropertyName { get; set; }
public bool IsInitialized
{
get
{
return (_action != null && _func != null);
}
}
Action<T> _action;
Func<T> _func;
public PropertyReference() …Run Code Online (Sandbox Code Playgroud) c# ×2
.net ×1
.net-4.0 ×1
dynamic ×1
linq ×1
reflection ×1
row-number ×1
silverlight ×1
twitter ×1
xaml ×1