Select Multiple Records Based on List of Id'S With Linq

Select multiple records based on list of Id's with linq

You can use Contains() for that. It will feel a little backwards when you're really trying to produce an IN clause, but this should do it:

var userProfiles = _dataContext.UserProfile
.Where(t => idList.Contains(t.Id));

I'm also assuming that each UserProfile record is going to have an int Id field. If that's not the case you'll have to adjust accordingly.

Linq one list to select multiple items from another list

You can try IEnumerable extension method Join

var result = products.Join(selectedIds, p => p.id, i => i, (p, i) => p).ToList()

Use Linq to get items in a list using a second list

You could use contains

var items = products.Where(p => idList.Contains(p.ID));

Linq select records that match a list of IDs

Yes, method List<T>.Contains will be translated into SQL IN operator:

var cust = db.Customers.Where(x => types.Contains(x.type_id)).ToList();

Generated query will look like:

SELECT * FROM Customers
WHERE type_id IN (@p0, @p1, @p2)

LINQ: Get unique rows by selected ID's based on latest time

What makes this complicated is that you want to retrieve rows with the most recent timestamps for multiple ships. I think the only way to do this will be to make a request for each SHIP_ID, and populate a list with each result.

var lastLocations = new List<SimpleAisRecords>();
var shipIds = new string[] { "1", "2", "3" };

using (var scope = _scopeFactory.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<AisRecordContext>();
foreach (var shipId in shipIds)
{
// Gets records for a SHIP_ID, orders by newest to oldest, and then grabs the first (newest) record
var query = dbContext.SimpleAisRecords
.Where(ship => ship.SHIP_ID == shipId)
.OrderderByDecending(ship => ship.TIMESTAMP)
.FirstOrDefault();

if (query != null)
{
lastLocations.Add(query);
}
}
}

By default it will return all columns from the associated table, so you'll have the LAT, LON, SPEED, HEADING, and all the other columns you need.

Select multiple records based on List of strings with LINQ

You just need to put in the actual expression that you want executed, not a lambda that defines how to do it. Replace (a => postedTillNames.Contains(a.Name)) with postedTillNames.Contains(a.Name).

The fact that you're providing the code in the context of a query expression means that the compiler will be wrapping the code up in a lambda for you. It's being wrapping it in a lambda twice (once by you, and once by the compiler), which is what's causing the problem.

Linq query to select records based on most recent sum

You probably need to produce a separate list with the cumulative sums. You can then use TakeWhile to take items until some condition is reached

var list = new[] { 1, 2, 3, 4, 5 };
var sum = 0;
var cummulativeSums = list.Select(v => sum += v).ToList();
var result= list.TakeWhile((_, index) => cummulativeSums[index] < 7).ToList();
// 1, 2, 3

Or you could do it all in one go by creating intermediate pair of values.

var list = new[] { 1, 2, 3, 4, 5 };
var sum = 0;
var result = list.Select(value => (sum += value, value))
.TakeWhile(pair => pair.Item1 < 7)
.Select(pair => pair.value)
.ToList();




Related Topics



Leave a reply



Submit