1

I am having a list of model which i populated with items. The below list is populated with items :-

List<Entry> list = new List<Entry>
{
    new Entry { EmployeeId = 1, EntryDate = '2016-11-01', InDate = '2016-11-01' },
    new Entry { EmployeeId = 1, EntryDate = '2016-11-05', InDate = '2016-11-05' },
    new Entry { EmployeeId = 2, EntryDate = '2016-11-01', InDate = '2016-11-01' }
};

Now I want to query a table Entry from database in a such a way that records should be matching with the above List with EmployeeId & EntryDate as parameter.

Entry table has same columns as in the code above.

If it was one field i can use contains but for multiple fields what should i do?

My results from database table should match the above List with 2 Columns matching EmployeeId & EntryDate.

8
  • You've only got a single entry in the list - it's not clear why you're using a list at all. It would help if you'd provide a minimal reproducible example. I'd also strongly advise against using strings to store dates. Commented Dec 6, 2016 at 9:20
  • There are many entries. I have given just a example. There are more than 500 records in the List. Commented Dec 6, 2016 at 9:21
  • An example is much more helpful if it actually demonstrates what you need - in this case, just two entries would be enough to make it clear. (Using a collection initializer with object initializers would make it even clearer.) Now, you haven't told us how you're querying the table. Is this LINQ to SQL? EF? Something else? And in-memory table? Commented Dec 6, 2016 at 9:22
  • Possible duplicate of Proper Linq where clauses Commented Dec 6, 2016 at 9:25
  • 1
    I've edited your sample code to use collection and object initializers to make it much more readable - and valid (your previous code wouldn't actually compile, as you were declaring the same local variable multiple times, and you hadn't indented it usefully - please pay attention to this sort of thing when writing questions). Commented Dec 6, 2016 at 9:31

2 Answers 2

3

You can achieve this through the .Contains operator and anonymous types in a way that is compatible with EF.

You need to first project your list to an anonymous type containing just those properties you want to compare.

var projection = from e in list 
                 select new { e.EmployeeId, e.EntryDate };

Then you can compare it to an identical projection in your query.

var query = db.Table.Where(e => projection.Contains(new { e.EmployeeId, e.EntryDate }));

This however generates SQL that has two parameters per item in list. How performant this is for 500 records, you will have to test. If this doesn't work, you can write a stored procedure with a table-valued parameter.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for Answer Tim. But in another scenario if i want to query not with table but on a List then will this work?
Do you mean "will this query work in memory as well as via SQL"? Then, yes.
I am getting error for above code as :- Delegate System.Func<Entry, int , bool> does not take 1 arguments.
This isn't working for me (EF6) - I get a System.NotSupportedException: 'Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.' What am I missing?
@Tyson This answer may be specific to SQL Server. If you're using another database, YMMV.
|
2

Logically, what you want is:

var query = db.Table.Where(e => list.Any(le => e.EmployeeId == le.EmployeeId &&
                                               e.EntryDate == le.EntryDate));

I don't know whether that will work with Entity Framework, but it's worth a try. (It would be fine in LINQ to Objects, for example.)

3 Comments

If you can express it in SQL it will work with LINQ to EF. This one won't. You could create a table-valued parameter. This is a clear sign though of ORM overuse. A stored procedure would be better
@PanagiotisKanavos: I suggest you add an answer, then I'll delete this one.
@JonSkeet Thanks. I will try this code & let you know.!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.