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

包含(guid)的子音速查询构造

  •  3
  • sestocker  · 技术社区  · 15 年前

    我有一张“笔记”桌。Notes支持一个线程级别——换句话说,您可以回复一个注释,但不能回复另一个回复。所以这张表看起来是这样的:

    CREATE TABLE [dbo].[Notes] (
     [NoteId] [uniqueidentifier] ROWGUIDCOL  NOT NULL DEFAULT (newid())
      CONSTRAINT [PK__Notes]
      PRIMARY KEY ([NoteId]),
     [ParentNoteId] UNIQUEIDENTIFIER NULL,
     [NoteText] NVARCHAR(MAX) NOT NULL,
     [NoteDate] DATETIME NOT NULL
        )
    

    因此,我使用亚音速主动记录获取所有“父”注释:

    var allNotes = (from n in Note.All()
                            where n.ParentNoteId == null
                            orderby n.NoteDate descending
                            select n)
                            .Skip((pageIndex - 1) * pageSize).Take(pageSize);
    

    接下来,我只需循环访问iqueryable,并填写注释guid的一般列表:

    List<Guid> noteList = new List<Guid>();
    foreach (var note in allNotes)
    {
         noteList.Add(note.NoteId);
    }
    

    最后,我尝试构造一个查询,以从原始查询中获取对笔记的所有答复:

    replies = from n in Note.All()
              where n.ParentNoteId != null && noteList.Contains(n.ParentNoteId.Value)
              select n
    

    我收到的错误是:“方法‘包含’不被支持”有什么想法吗?

    编辑:我尝试转换为如下字符串:

    List<String> noteList = new List<String>(); 
    foreach (var note in allNotes) 
    { 
         noteList.Add(note.NoteId.ToString()); 
    } 
    replies = (from n in Note.All() 
              where n.ParentNoteId != null && 
              noteList.Contains(n.ParentNoteId.Value.ToString()) select n); 
    

    与以前相同的错误消息。

    2 回复  |  直到 15 年前
        1
  •  10
  •   VladV    15 年前

    似乎,列表<gt;。子音速不支持包含。

    但是,支持IEnumerable<gt;。包含,因此您可以尝试以下操作:

    IEnumerable<string> noteListEnumerable = noteList;
    
    replies = from n in Note.All()
              where n.ParentNoteId != null && noteListEnumerable.Contains(n.ParentNoteId.Value)
              select n
    
        2
  •  1
  •   user1151    15 年前

    我认为如果设置为一个字符串,它应该可以工作——contains只对字符串值实现,但我知道我们确实可以工作。