在一个实体LINQ语句中加入集合[英] Join collection inside an Entity LINQ statement

本文是小编为大家收集整理的关于在一个实体LINQ语句中加入集合的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这个问题与 LINQ-to-entity 相关.

我发布了 类似问题,但没有答案就让人困惑,所以我提供了一个例子和一个新的呼救声.

我有一个"颜色"类,其中包含一个 ObservableCollection,它有两个成员,Index 和 Name,填充如下:

0 - 红色
1 - 蓝色
2 - 绿色

我有一个数据库表,其中包含我最喜欢的颜色的整数列表.我想根据存储在数据库中的索引值返回一个查询,其中包含我最喜欢的颜色的整数值和匹配的名称(由 observablecollection 返回).

这个单独的语句可以正常工作并返回我的颜色名称:-

string ColorName = Colors.Names.Where(x => x.Index == 1).FirstOrDefault().Name;

但是当包含在 LINQ-to-entity 查询中时:-

var query = from c in context.FavoriteColor
select (new Item
    {
        Id = c.Id,
      ColorName = Colors.Names.Where(x => x.Index == c.ColorIndex).FirstOrDefault().Name
    });

我收到此错误:

<块引用>

无法创建"blah blah"类型的常量值.只有原始在此支持类型('例如 Int32、String 和 Guid')上下文.

我知道该对象可能在 LINQ 语句中返回,但我认为通过在末尾指定 .Name 表示法,它是一个字符串成员,它会使用它并将其分配给"ColorName",可惜不是.

推荐答案

您正试图在数据库查询中使用 CLR 对象.LINQ-to-Entities 是说你不能这样做.查询完成后获取颜色名称.

var dbItems = context.FavoriteColor.Select(c => new {
    c.Id,
    c.ColorIndex
).ToList();

var items = dbItems.Select(item => new Item {
    Id = item.Id,
    ColorName = Colors.Names.Where(x => x.Index == item.ColorIndex).First().Name
})

这里的想法是,对 ToList 的调用会命中数据库并在 CLR 对象中返回结果.然后可以像使用任何其他对象一样使用该对象.

本文地址:https://www.itbaoku.cn/post/1557093.html

问题描述

This problem related to LINQ-to-entity.

I posted a similar question but it got confusing without an answer, so I am providing an example and a new shout for help.

I have a class "Colors" containing an ObservableCollection which has two members, Index and Name and populated like :

0 - Red
1 - Blue
2 - Green

and I have a database table containing a list of integers of my favorite colors. I would like to return a query with both the integer value of my favorite color and also the matching name (returned by the observablecollection) based on the index value stored in the database.

This statement in isolation works fine and returns my color name :-

string ColorName = Colors.Names.Where(x => x.Index == 1).FirstOrDefault().Name;

but when included within the LINQ-to-entity query :-

var query = from c in context.FavoriteColor
select (new Item
    {
        Id = c.Id,
      ColorName = Colors.Names.Where(x => x.Index == c.ColorIndex).FirstOrDefault().Name
    });

I get this error :

Unable to create a constant value of type 'blah blah'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

I understand that maybe the object is being returned within the LINQ statement but I thought by specifying the .Name notation on the end, which is a string member, it would use that and assign it to "ColorName", alas not.

推荐答案

You are trying to use a CLR object in a database query. LINQ-to-Entities is saying you can't do that. Get the color name after the query has completed.

var dbItems = context.FavoriteColor.Select(c => new {
    c.Id,
    c.ColorIndex
).ToList();

var items = dbItems.Select(item => new Item {
    Id = item.Id,
    ColorName = Colors.Names.Where(x => x.Index == item.ColorIndex).First().Name
})

The idea here is that the call to ToList hits the database and returns the results in a CLR object. That object can then be used like any other object.