开发者

Why won't this LINQ join statement work?

I have this LINQ-query:

    // types...
    LinkedList<WeightedItem> itemScores = new LinkedList<WeightedItem>();

    var result = from i in _ctx.Items
                 join s in itemScores on i.Id equals s._id
                 orderby s._score descending
                 select new ItemSearchResult(i, s._score);

    // this fails:
    return result.ToList();

Which is generating this error:

Unable to create a constant value of type 'System.Collections.Generic.IEnumerable`1'.

Only primitive types ('such as Int32, String, and Guid') are supported in this context.

[EDIT] Here's the code of WeightedItem:

public class WeightedItem
{
    public int _id;
    public decimal? _score;

 开发者_Go百科   public WeightedItem(int id, decimal? score)
    {
        _id = id;
        _score = score;
    }
}

Can you see what I've done wrong? The code compiles perfectly and both the _ctx.Items and itemScores contains proper values.


Yes, it would compile fine - the problem is that it can't translate it into SQL. When you reference "local" values, the entity framework has to work out what to do with them when it needs to create a SQL query. It basically can't cope with doing a join between an in-memory collection and a database table.

One thing which might work would be to use Contains instead. I don't know whether LinkedList<T> will work for this, but I believe List<T> does, at least in LINQ to SQL:

List<int> requiredScoreIds = itemScores.Select(x => x._id).ToList();

var tmp = (from i in _ctx.Items
           where requiredScoreIds.Contains(i.Id)
           orderby s._score descending
           select i).AsEnumerable();

// Now do the join in memory to get the score
var result = from i in tmp
             join s in itemScores on i.Id equals s._id
             select new ItemSearchResult(i, s._score);

Now that's doing a join in the in-memory query, which is somewhat unnecessary. You could instead use a dictionary:

List<int> requiredScoreIds = itemScores.Select(x => x._id).ToList();

var tmp = (from i in _ctx.Items
           where requiredScoreIds.Contains(i.Id)
           orderby s._score descending
           select i).AsEnumerable();

// Create a map from score ID to actual score
Dictionary<int, decimal?> map = itemScores.ToDictionary(x => x._id,
                                                        x => x._score);

var result = tmp.Select(i => new ItemSearchResult(i, map[i.Id]));


You can't join between an in memory list and a queriable object. You need to do something like this:

var criteria = itemScores.Select(x => x._id).ToList();
var result_tag = (from i in _ctx.Items
                 where criteria.Contains(i.ID)
                 select i).ToList();
var result = from i in result_tag
             join s in itemScores on i.ID equals s._id
             orderby s._score descending
             select new ItemSearchResult(i, s._score);


Just in case the table represented by _ctx.Items is not a big one and you don't care about loading all the table in memory and then filter it in memory, you can simply swap the order of the items in the join statement, as in the following snippet:

LinkedList<WeightedItem> itemScores = new LinkedList<WeightedItem>();

var result = from s in itemScores
             join i in _ctx.Items on s._id equals i.Id
             orderby s._score descending
             select new ItemSearchResult(i, s._score);

return result.ToList();

In the original statement the Queryable extension method was invoked:

IQueryable<TResult> Queryable.Join<TOuter, TInner, TKey, TResult>(
        this IQueryable<TOuter> outer,
        IEnumerable<TInner> inner,
        Expression<Func<TOuter, TKey>> outerKeySelector,
        Expression<Func<TInner, TKey>> innerKeySelector,
        Expression<Func<TOuter, TInner, TResult>> resultSelector
)

while in the swapped one the Enumerable extension method is invoked:

IEnumerable<TResult> Enumerable.Join<TOuter, TInner, TKey, TResult>(
        this IEnumerable<TOuter> outer,
        IEnumerable<TInner> inner,
        Func<TOuter, TKey> outerKeySelector,
        Func<TInner, TKey> innerKeySelector,
        Func<TOuter, TInner, TResult> resultSelector
)

so in the last statement the full _ctx.Items table is loaded in memory and then joined, via Linq to Objects, to the itemScores list (I don't know about LinkedList, I tried it with List).

I added this answer mainly because someone could type the join in the reverse order and have it work without even realize what is going to happen in the database.

I wouldn't suggest to join in this way, though it can be useful for backoffice applications whenever the involved tables are made up of few records and the application doesn't suffer a relevant performance worsening. This solution, after all, keeps the code cleaner.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜