0

I have a list of this model: Model1

public class Model1//: IConvertible
{
    public int p1 { get; set; }

    public string p2{ get; set; }
}

And I'm trying to do a linq query from another table to get data using a list of Model1

List<Model1> lList = GetModel();
var test = Model2.Where(p => lList .Any(l => p.p1== l.p1 && p.p2== l.p2)).ToList();

What am I missing?

Johan Sánchez
  • 165
  • 3
  • 17
  • What is `Model2`? – Uwe Keim May 11 '19 at 17:49
  • What do you want to achieve? Are you getting any error or what else? – Kundan Singh Chouhan May 11 '19 at 17:51
  • `Model2` is a model who contains those properties of Model1, it also has more columns. – Johan Sánchez May 11 '19 at 17:56
  • @KundanSinghChouhan **Unable to create a constant value of type 'Model1'. Only primitive types or enumeration types are supported in this context.** that's the error. – Johan Sánchez May 11 '19 at 17:59
  • This is another instance of [this question](https://stackoverflow.com/q/26198860/861716). Somebody did an effort to build a [library](http://tsherlock.tech/2018/03/20/joining-in-memory-list-to-entity-framework-query/) which will join with value tuples, but it was far from mature [last time I tried](https://codereview.stackexchange.com/a/190364/7251). – Gert Arnold May 11 '19 at 18:52

1 Answers1

0

I concatenated p1 + p2 with Linq

   var stringKeys = lList.Select((e, i) => lList[i].p1.ToString() + "_" + lList[i].p2).ToList();

Then to use this list in the another query is:

   var result = Model2.Where(e => stringKeys.Contains(e.p1 + "_" + e.p2))
                .ToList();
Johan Sánchez
  • 165
  • 3
  • 17