How can I write the “Where” clause in the following LINQ to SQL Query?

Try: Where(rules. Select(r => r. ID).

Contains(arr. AuditRuleEnterpriseID. AuditID)) or, in query syntax where rules.

Select(r => r. ID). Contains(arr.

AuditRuleEnterpriseID. AuditID).

Try: . Where(rules. Select(r => r.ID).

Contains(arr. AuditRuleEnterpriseID. AuditID)) or, in query syntax where rules.

Select(r => r. ID). Contains(arr.

AuditRuleEnterpriseID. AuditID).

Indeed, LINQ to SQL expands that to an IN expression. – Richard Jul 2 '09 at 15:41 thanks jason. I appreciate it.

– KingNestor Jul 2 '09 at 15:50.

You define LINQ to SQL queries by using the same syntax as you would in LINQ. The only difference is that the objects referenced in your queries are mapped to elements in a database. For more information, see Introduction to LINQ Queries.

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. Â More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADOÂ provider.

 The ADO provider returns query results as a DataReader. The LINQ to SQL provider translates the ADO results to an IQueryable collection of user objects. The following illustration depicts this general flow.

The following table shows the similarities and differences between LINQ and LINQ to SQL query items.

Keep in mind I'm pretty new to link, and I don't think this is the best way: var collection = from d in select d; collection = collection. GroupBy(x => x. Date).

Select(x => x.Last()) Basically just group on the date and take the last one. I couldn't think of a good way to do this in the LINQ command, so I just selected all the rows - anyone know to combine these lines?

Keep in mind I'm pretty new to link, and I don't think this is the best way: var collection = from d in select d; collection = collection. GroupBy(x => x. Date).

Select(x => x.Last()); Basically just group on the date and take the last one. I couldn't think of a good way to do this in the LINQ command, so I just selected all the rows - anyone know to combine these lines?

Yes, you can simply replace collection with TABLE in line 2. – leppie Mar 20 '09 at 4:52.

If your dates included time, then you could: var locations = from l in mycontext. Locations where l. Date = mycontext.

Locations . Where(l2=>l. Location == l2.

Location) . Max(l2. Date) select l; Alternatively, assuming ID is an identity and the records are inserted in the table when they occurred(your example seems to hint so), you can do: var locations = from l in mycontext.

Locations where l. ID = mycontext. Locations .

Where(l2=>l. Location == l2. Location) .

Max(l2. ID) select l.

Bobby Shaftoe I believe that he was looking for the latest entry rather than the latest date for every location. If my assumption is correct, it would be more like SELECT max(ID),location,date FROM tablename GROUP BY location.

Well, he said date entry. However, if his data is consistent, it looks like it would be the same thing, perhaps. But I stick with what I have based on the question was written, you might be right though.

– BobbyShaftoe Mar 20 '09 at 4:34 Oh, and there is a mistake in this one. The DB will complain that "date" is not in group by clause or in an aggregate function. – BobbyShaftoe Mar 20 '09 at 4:35 He has the result he expects here - it's the full row.

Not sure if it was there originally though. – Damovisa Mar 20 '09 at 4:36 yes the result is a full row – upa Mar 20 '09 at 4:41.

To get the whole row with the max date for each location, I think the SQL is: SELECT t1. Id, t1. Location, t1.

Salary, t1. Date FROM YourTable t1, (SELECT Location, MAX(Date) AS MaxDate FROM YourTable GROUP BY Location) t2 WHERE t1. Location = t2.

Location AND t1. Date = t2. MaxDate Please correct me if I'm wrong!

– upa Mar 20 '09 at 4:56 Yeah, great question :) The short answer is I don't know Linq well enough. I know linq tends to mirror sql somewhat, but I don't know the exact syntax (if there is one) for grouping and joining. You may have to follow Steve Willard's example - select everything and pare it down using expressions.

– Damovisa Mar 20 '09 at 5:58.

Actually, this is not so straight forward if I read this question correctly. Because Los Angeles has 2 entries on the Max date there appears to be a further rule that in that situation the max ID should be selected. In that case the SQL becomes a little more complex, 2 groupings are required.... select id, location, salary, date from locations where id in (select max(id) as MaxID from locations where date in ( Select Max(Date) as MaxDate from Locations group by Location) Group by location) The corresponding Linq query, would also require 2 groupings... var qry = from a in locations group a by a.

Location into grp from be in grp where b. Date == grp. Max(o => o.

Date) group be by b. Location into grp2 from c in grp2 where c. ID == grp2. Max(obj => obj.

ID) orderby c. ID select c.

SELECT location, max(date) FROM yourtable GROUP BY location EDIT: I didn't see the results being asked for or not sure if it was there originally: SELECT yourtable. Id, yourtable. Location, yourtable.

Salary, yourtable. Date FROM yourtable INNER JOIN (SELECT yourtable. Location, MAX(yourtable.

Date) as thedate FROM yourtable GROUP BY yourtable. Location) AS MYRESULT on MYRESULT. Thedate = yourtable.

Date AND MYRESULT. Location = yourtable. Location Haven't tested this though.

The recommended way to write most queries is to use query syntax to create query expressions. The following example shows three query expressions. The first query expression demonstrates how to filter or restrict results by applying conditions with a where clause.

It returns all elements in the source sequence whose values are greater than 7 or less than 3. The second expression demonstrates how to order the returned results.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions