Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

join list with linq-to-sql query

Tags:

c#

linq

I have list of MyObject that looks like this:

public class MyObject{
  public int FruitID {get;set;}
  public string FruitName {get;set;}
}

List<MyObject> TheList = new List<MyObject>();

This list is populated with a linq-to-sql query. I'm looking to create a join between this list and a table that contains FruitID as its foreign key.

The table HarvestTimes looks like this:

FruitID   |   HarvestDatetime  |   RipeFactor
   3      |        3/4/2011    |       2
   3      |        4/5/2011    |       4
   3      |        5/5/2011    |       3
   4      |        3/21/2011   |       2
   4      |        4/10/2011   |       2
   4      |        5/10/2011   |       2

This is what I have so far:

var TheQuery = (from list in TheList
                join fruit in MyDC.HarvestTimes on
                list.FruitID equals fruit.FruitID
                where ....
                select new MyObject{... }).ToList();

I'm have some trouble with the Where clause. How do I get only the Fruit where the RipeFactor was always 2. For instance, Fruit 3 has a RipeFactor of 2 but also has 4 and whereas only Fruit4 has only 2s. I tried with Contains but both fruits come up.

Thanks for your suggestions.

like image 716
frenchie Avatar asked May 11 '11 15:05

frenchie


People also ask

Can I join a table to a list using LINQ?

Accepted Answer You probably found out that you can't join an Entity Framework LINQ query with a local list of entity objects, because it can't be translated into SQL. I would preselect the database data on the account numbers only and then join in memory.

How use inner join in LINQ query?

A simple inner join that correlates elements from two data sources based on a simple key. An inner join that correlates elements from two data sources based on a composite key. A composite key, which is a key that consists of more than one value, enables you to correlate elements based on more than one property.

Can we do left join in LINQ?

A left outer join is a join in which each element of the first collection is returned, regardless of whether it has any correlated elements in the second collection. You can use LINQ to perform a left outer join by calling the DefaultIfEmpty method on the results of a group join.

Can we do joins in LINQ?

In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables. In LINQ, explicit join clauses are only required when two source sequences are not tied by any relationship.


2 Answers

Assuming there is a Relationship between the tables HaverstTime and Fruit:

var TheQuery = MyDC.HarvestTimes
    .Where(p => TheList.Select(q => q.FruitID).Contains(p.FruitID))
    .GroupBy(p => p.Fruit)
    .Where(p => p.All(q => q.RipeFactor == 2))
    .Select(p => p.Key);

This will create a IEnumerable<Fruit> which I think can be easily converted to MyObject.

Update: Oops I forgot to add TheList.Select(q => q.FruitID). That's why it didn't compile. Sorry =)

Update2: Do the same, considering Ripefactor = 2 and 3

var TheQuery = MyDC.HarvestTimes
    .Where(p => TheList.Select(q => q.FruitID).Contains(p.FruitID))
    .GroupBy(p => p.Fruit)
    .Where(p => p.All(q => q.RipeFactor == 2 || q.RipeFactor == 3))
    .Select(p => p.Key);
like image 160
Francisco Avatar answered Oct 13 '22 00:10

Francisco


I think this would work

var fruit = (from list in TheList
             join fruit in
               (from fr in MyDc.HarvestTimes
                group fr by fr.FruitID into fg
                where !fg.Any(f => f.RipeFactor != 2)
                select fg)
             on list.FruitID equals fruit.Key
             select new MyObject{... }).ToList();

Update - If you only want to return the distinct list of FruitIDs you need to select fg.Key instead of fg

var fruit = (from list in TheList
             join fruit in
               (from fr in MyDc.HarvestTimes
                group fr by fr.FruitID into fg
                where !fg.Any(f => f.RipeFactor != 2)
                select fg.Key)
              on list.FruitID equals fruit
              select new MyObject{... }).ToList();
like image 26
Aducci Avatar answered Oct 12 '22 23:10

Aducci