简体   繁体   English

Linq查询投影ID不是名字

[英]Linq query projecting Id's not names

I have a simple setup. 我有一个简单的设置。

FRUIT Table
Id     Name
1      Gala Apples
2      Navel Oranges
3      Peach
4      Mandarin Oranges
5      Kiwi
6      Fuji Apples

INTERSECT TABLE
FruitId     CrossRefFruitId
  1              6
  2              4

So if the user is looking at Gala Apples (1) they may also be interested in Fuji Apples (6). 因此,如果用户正在看Gala苹果(1),他们可能也会对Fuji苹果(6)感兴趣。

I have a simple model that returns the Fruit 我有一个返回水果的简单模型

Model
public class FruitCategory
{
    public int Id { get; set; }
    public string FruitName { get; set; }
}

EF:
public IEnumerable<FruitCategory> GetFruitbyId(int id)
{
  return _context.FruitTable.Where(q => q.FruitId == id);
}

This works fine but now I also want to add the "SeeAlso" fruit. 效果很好,但现在我也想添加“ SeeAlso”水果。 So I create a crossref modal and a new field in my Model. 因此,我在模型中创建了一个crossref模态和一个新字段。

CrossReff Model
public class FruitCrossRef
{
    public int Id { get; set; }
    public string CrossRefName { get; set; }
}

Model
public class FruitCategory
{
    public int Id { get; set; }
    public string FruitName { get; set; }
    public List<FruitCrossRef> SeeAlsoFruits {get; set;}
}

Now I come to my difficulty....how to get a LINQ projection that will populate this model. 现在我遇到了困难。...如何获得将填充此模型的LINQ投影。

Since I don't know how to write this I open LINQPAD and start hacking and googling. 由于我不知道该怎么写,因此我打开LINQPAD并开始黑客和谷歌搜索。

So far this is what I have come up with but it returns the MATCHING id in the intersect table but what I want is to return the CrossReferenced ID and the FruitName in the Fruit Table. 到目前为止,这是我想出的,但是它在相交表中返回了匹配 ID,但我要返回的是水果表中的CrossReferenced ID和FruitName。

var seeAlso =
(from frt in FruitTable
where frt.Id == 1
select frt.Id)
.Intersect
(from frtCross in IntersectTable
select frtCross.FruitId);

seeAlso.Dump();

Now I can see a path where I can get the job done by making several loops getting the seealso references and then for each one going back to the Fruit table and getting that record...however it seems there ought to be a way to leverage the power of the relationship and project my fully populated model??? 现在,我看到了一条路径,可以通过执行几个循环来获取seesee引用,然后为每个循环返回Fruit表并获取该记录来完成工作……但是似乎应该有一种方法可以利用关系的力量并投射出我完全填充的模型???


Code Correction 代码更正

For anyone else who may come across this there were a couple syntax errors in the answer but the answer was still exactly what I wanted. 对于可能遇到此问题的其他任何人,答案中都有一些语法错误,但答案仍然正是我想要的。

var seeAlso =
  (from frt in FruitTable
    join intsec in IntersectionTable
    on frt.Id equals intsec.CrossRefFruitId
    where intsec.FruitId == 1
    select frt);

seeAlso.Dump();

Remember this was written for Linq Pad a little more tweaking is needed for production code. 记住,这是为Linq Pad编写的,需要对生产代码进行更多调整。

What you ultimately want is a list of FruitItems that are also of interest based on some other fruit, given that fruits Id. 您最终想要的是一个FruitItems列表,考虑到水果Id,这些列表也基于其他一些水果而引起关注。 Therefore rather than selecting the fruit corresponding to the Id you want, you should select the Fruits that join to the Intersection table with that Id. 因此,您应该选择与该ID联接到“交集”表的“水果”,而不是选择与所需ID相对应的水果。 For example. 例如。

var seeAlso =
    (from frt in FruitTable
     join intsec in IntersectionTable
        on frt.Id = intsec.CrossRefFruitId
     where intsec.FruitId == 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM