小编Jef*_*ber的帖子

如何将行号投射到Linq查询结果中

如何将行号投影到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 ++"行引发了以下编译时异常:

"表达式树可能不包含赋值运算符"

linq row-number

34
推荐指数
1
解决办法
4万
查看次数

如何使用.Net阅读公共Twitter提要(在Windows Phone上)

我正在尝试阅读用户的公共推特状态,以便我可以在我的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中的:功能.一旦我这样做,安全例外就消失了.

.net c# twitter windows-phone-7

8
推荐指数
1
解决办法
9453
查看次数

为什么Silverlight不会处理我的自定义浮动属性的转换

在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中的字符串表示转换为浮点数.

有人有主意吗?

silverlight xaml typeconverter

5
推荐指数
1
解决办法
466
查看次数

在c#中按名称获取和保持属性引用的最佳方法是什么?

我想知道是否有更好的方法(比我目前正在做的)只使用对象和属性字符串名来获取并保持对另一个对象中的属性的引用.特别是,使用.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# reflection dynamic .net-4.0

2
推荐指数
1
解决办法
1274
查看次数