代码之家  ›  专栏  ›  技术社区  ›  martin

Linq to SQL奇怪的连接问题

  •  0
  • martin  · 技术社区  · 17 年前

    我有一个简单的数据库,有两个表。用户和配置。用户有一个外键将其链接到特定配置。

    我遇到了一个奇怪的问题,下面的查询总是导致与配置表的内部联接,而不管第二个参数值是什么。据我所知,即使对象初始化的“userconfiguration=”部分是有条件的,Linq也看不到这一点,并确定在任何情况下都遵循关系。

    如果我真的删除了最后的初始化,那么整个过程就会如预期的那样工作。当loadconfiguration==false时,它不进行内部联接;当loadconfiguration==true时,它进行联接。

    有人对此有什么想法吗?这个语法就是不起作用吗?我现在唯一的想法是用一个基本的if语句包装返回——我只是想避免重复的行。

    public UserAccount GetByUsername(string username, bool loadConfiguration)
    {
        using (Database database = new Database())
        {
            if (loadConfiguration)
            {
                DataLoadOptions loadOptions = new DataLoadOptions();
                loadOptions.LoadWith<User>(c => c.Configuration);
                database.LoadOptions = loadOptions;
            }
    
            return (from c in database.Users
                    where c.Username == username
                    select new UserAccount
                    {
                        ID = c.ID,
                        ConfigurationID = c.ConfigurationID,
                        Username = c.Username,
                        Password = c.Password.ToArray(),
                        HashSalt = c.HashSalt,
                        FirstName = c.FirstName,
                        LastName = c.LastName,
                        EmailAddress = c.EmailAddress,
    
                        UserConfiguration = (loadConfiguration) ? new ApplicationConfiguration
                        {
                            ID = c.Configuration.ID,
                            MonthlyAccountPrice = c.Configuration.MonthlyAccountPrice,
                            TrialAccountDays = c.Configuration.TrialAccountDays,
                            VAT = c.Configuration.VAT,
                            DateCreated = c.Configuration.DateCreated
    
                        } : null
    
                    }).Single();
        }
    }
    

    事先谢谢,

    马丁。

    3 回复  |  直到 17 年前
        1
  •  0
  •   leppie    17 年前

    我不认为它会像那样工作。

    我建议将其分为两个不同的查询。

    可能有更好的方法,但它需要更多的“水管”。

        2
  •  0
  •   liggett78    17 年前

    不,这样不行。我经常碰到类似的问题。原因与编译时的表达式和运行时的条件有关。

    您可以执行2个查询,或者如果您不介意加入到配置中,而不管loadconfiguration参数如何,您可以使用:

    var q = (from c in database.Users
                    where c.Username == username
                    select c).Single();
    

    然后对结果上的对象使用Linq。

        3
  •  0
  •   Pop Catalin    17 年前

    将.single()替换为singleOrDefault(),Linq将切换到左侧外部联接。我不知道在你的情况下它是否会这样做,但在某些情况下它会这样做。

    edit dint's see the single was for the entire query而非configuration part:

    试试这个:

         UserConfiguration = (loadConfiguration && c.Configuration != null) ? new ApplicationConfiguration
         {
              ID = c.Configuration.ID,
              MonthlyAccountPrice = c.Configuration.MonthlyAccountPrice,
              TrialAccountDays = c.Configuration.TrialAccountDays,
              VAT = c.Configuration.VAT,
              DateCreated = c.Configuration.DateCreated
          } : null