简体   繁体   中英

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).

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. So I create a crossref modal and a new field in my Model.

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.

Since I don't know how to write this I open LINQPAD and start hacking and googling.

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.

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???


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.

What you ultimately want is a list of FruitItems that are also of interest based on some other fruit, given that fruits 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. For example.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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