【问题标题】:LINQ: Concating two lists and creating a new one doesnt seem to be bindableLINQ:连接两个列表并创建一个新列表似乎不可绑定
【发布时间】:2013-09-28 20:12:48
【问题描述】:

我正在尝试连接两个列表并获得一个新的匿名列表,然后将其绑定到 silverlight 中的网格以用于应用:

        var lst = 
            from games in AppLogic.CurrentApp.GAMES.Entities
            from playedGames in AppLogic.CurrentApp.PLAYEDGAMES.Entities.Where(f => f.GameID == games.ID).DefaultIfEmpty()
            select new
            {
                ID = games.ID,
                Date = games.Date,
                MaxPoints = games.MaxPoints
            };

        dgGames.ItemsSource = lst;

但我总是得到一个空引用异常。 我在另一个 stackoverflow 线程中看到了这种语法(遗憾的是我没有标记 url),但它似乎并没有以这种方式工作。 我是在做一些根本错误的事情,还是不能像这样绑定新列表?

绑定是这样的:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <data:DataGrid x:Name="dgGames">
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Visibility="Collapsed" Binding="{Binding ID}" />
                <data:DataGridTextColumn Header="Datum" Binding="{Binding Date}" />
                <data:DataGridTextColumn Header="Punkte" Binding="{Binding MaxPoints}" />
                <!--<data:DataGridTextColumn Header="Gewinner" Binding="{Binding Winner}" />-->
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>

P.S.:如您所见,我什至没有从左侧加入“PlayedGames”获取数据,但它似乎仍然无法正常工作。

如果有什么愚蠢的错误,请提前道歉。我从找到的一些示例中修改了代码,因此我无法确定它们是否有效。

非常感谢所有回答!

马蒂亚斯

【问题讨论】:

  • 由于您没有从左连接的“PlayedGames”中获取数据,您是否尝试删除该行以查看查询是否有效?

标签: linq silverlight binding


【解决方案1】:

尝试以这种方式重写您的左连接。

var lst = from games in AppLogic.CurrentApp.GAMES.Entities
    join playedGames in AppLogic.CurrentApp.PLAYEDGAMES.Entities
    on game.ID equals games.GameID into JoinedGames
    from subGames in JoinedGames.DefaultIfEmpty()
    select new                          
    {
       ID = games.ID,
       Date = games.Date,
       MaxPoints = games.MaxPoints                      
    };

dgGames.ItemsSource = lst.ToList();

【讨论】:

  • 嗯,我内置了一个 try catch,发现了这个错误消息: Message = "The query contains references to items defined on a different data context."似乎不可能合并到这样的列表中。制作一个自己的对象只是为了组合它们是否合适?
  • 对不起,为什么在 2 个数据上下文中,它们来自不同的数据库?
  • 根据我的 silverlight 教程,我必须创建两个这样的类: public class PLAYEDGAMES:DataContext { private const string CONNECTION_STRING = "isostore:/tblPLAYEDGAMEs.sdf";公共表 实体; public PLAYEDGAMES() : base(CONNECTION_STRING) { } } 所以一个用于玩过的游戏,一个用于游戏
  • 如果它们位于 2 个不同的数据库中,那么您不能以这种方式组合查询。您将需要添加 ToList(),不确定是否需要为两个实体列表执行此操作。如果它不起作用,请尝试 AppLogic.CurrentApp.GAMES.Entities.ToList() 也将其应用于 PLAYEDGAMES.Entitites。
  • 所以我只是在 page_load 事件中设置了列表,并从基础上列出了所有内容。 Doenst 似乎正确,但有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
相关资源
最近更新 更多