0

我有以下查询sql

SELECT [definition],[pos]
  FROM [WordNet].[dbo].[synsets]
  where synsetid in(SELECT [synsetid] FROM [WordNet].[dbo].[senses] 
  where wordid = (select [wordid]FROM [WordNet].[dbo].[words]
  where lemma = 'searchString'))

我已经为 sql to linq 试过这个:

long x = 0;

if (!String.IsNullOrEmpty(searchString))
{
    var word = from w in db.words
               where w.lemma == searchString
               select w.wordId;
    x = word.First();

   var sence = from s in db.senses
               where (s.senseId == x)
               select s;
   var synset = from syn in db.synsets
                where sence.Contains(syn.synsetId)
                select syn;

但我收到以下错误sence.Contains()

  Error1:Instance argument: cannot convert from
  'System.Linq.IQueryable<WordNetFinal.Models.sense>' to
  'System.Linq.ParallelQuery<int>'  
4

3 回答 3

2

下面的代码:

var sence = from s in db.senses
               where (s.senseId == x)
               select s;

返回类型的对象:WordNetFinal.Models.sense,但在where sence.Contains(syn.synsetId)您尝试在其中搜索它syn.synsetId是一个整数。

所以你应该把上面的代码改成:

var sence = from s in db.senses
                   where (s.senseId == x)
                   select s.senseId;
于 2013-04-14T18:52:02.233 回答
0

x 似乎是 Word 类型,而不是 Id 的类型(可能是 int 或 long)。

于 2013-04-14T18:50:43.440 回答
0

您正在将整sense行与 a进行比较synsetId,这是不正确的。您还将原始查询拆分为两个单独的查询,方法是使用First()到目前为止触发表达式的评估。如果在 中有重复项时您可以忍受不返回 SQL 错误,则words可以将查询编写为如下内容;

if (!String.IsNullOrEmpty(searchString))
{
    var wordIds = from word in db.words
                  where word.lemma == searchString
                  select word.wordId;

    var synsetIds = from sense in db.senses
                    where wordIds.Contains(sense.wordId)
                    select sense.synsetId;

    var result = (from synset in db.synsets
                  where synsetIds.Contains(synset.synsetId)
                  select new {synset.definition, synset.pos}).ToList(); 
}

为整个ToList()查询触发一次评估。

您也可以使用更简单的连接来完成;

var result = (from synset in db.synsets
              join sense in db.senses on synset.synsetId equals sense.synsetId
              join word in db.words on sense.wordId equals word.wordId
              select new {synset.definition, synset.pos}).ToList();
于 2013-04-14T19:09:43.370 回答