如何在选择新对象时使用LINQ进行分配



我有一个名为GameImageGameTone的数据契约。我正在尝试加入这两个实体,并将0-11之间的唯一随机位置分配给图像/色调关联。我可以连接这些表,但不确定在LINQ lambda表达式中创建对象时是否有分配位置的方法。

            // Need random positions from 0-11 to to be associated to an image/tone
            var positions = Enumerable.Range(0, 11).Shuffle().ToList();
            // Associate image/tones
            imageToneData = game.GameImages.Shuffle()
                .Join(game.GameTones, gi => gi.GameId, gt => gt.GameId, (gi, gt) => new ImageToneData
                {
                    Image = new ImageData()
                    {
                        ImageFileName = gi.Image.ImageFileName,
                        ImageId = gi.ImageId
                    },
                    Tone = new ToneData()
                    {
                        ToneFileName = gt.Tone.ToneFileName,
                        ToneId = gt.ToneId
                    },
                    Position = // What goes here?
                });

这些是我的数据合约

[DataContract]
public class ImageToneData
{
    [DataMember]
    public ImageData Image { get; set; }
    [DataMember]
    public ToneData Tone { get; set; }
    [DataMember]
    public int Position { get; set; }
}
[DataContract]
public class ImageData
{
    [DataMember]
    public int ImageId { get; set; }
    [DataMember]
    public string ImageFileName { get; set; }
}

}

[DataContract]
public class ToneData
{
    [DataMember]
    public int ToneId { get; set; }
    [DataMember]
    public string ToneFileName { get; set; }
}
  var positions = Enumerable.Range(0, 11).OrderBy(a => Guid.NewGuid()).ToList();
  // Associate image/tones
            imageToneData = game.GameImages.Shuffle()
                .Join(game.GameTones, gi => gi.GameId, gt => gt.GameId, (gi, gt) => new ImageToneData
                {
                    Image = new ImageData()
                    {
                        ImageFileName = gi.Image.ImageFileName,
                        ImageId = gi.ImageId
                    },
                    Tone = new ToneData()
                    {
                        ToneFileName = gt.Tone.ToneFileName,
                        ToneId = gt.ToneId
                    },
                    Position = positions.First()
                });

相关内容

  • 没有找到相关文章

最新更新