473,909 Members | 4,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linq query help

Hello,

I need some assistance with a LINQ query. I've got a simple query:

var q = from t in db.Table1 select t;

Based on user input I'm adding some where clauses:

if (condition1)
q = q.Where(t =t.Field1==1);
if (condition2)
q = q.Where(t =t.Field2==2);

Table1 has a sub table. Based on a condition I want find the Table1 records
that have one or more Table2 records. If I were using sql I would do
something like this:

select * from Table1 t1 where exists (select * from Table2 t2 where
t1.id=t2.id and t2.field1='some value')

How can I do this in LINQ given that I have added the above where clauses
based on those conditions?

Thanks,
Nick
Jul 23 '08 #1
2 1406
Nick wrote:
Hello,

I need some assistance with a LINQ query. I've got a simple query:

var q = from t in db.Table1 select t;

Based on user input I'm adding some where clauses:

if (condition1)
q = q.Where(t =t.Field1==1);
if (condition2)
q = q.Where(t =t.Field2==2);

Table1 has a sub table. Based on a condition I want find the Table1 records
that have one or more Table2 records. If I were using sql I would do
something like this:

select * from Table1 t1 where exists (select * from Table2 t2 where
t1.id=t2.id and t2.field1='some value')

How can I do this in LINQ given that I have added the above where clauses
based on those conditions?
Use the Queryable.Any extension method:
q=q.Where(t=>t. Table2s.Any(t2= >t2.Field1==som eValue));

FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jul 24 '08 #2
Thanks a lot for your help. This was exactly what I was looking for.

Thanks,
Nick
Jul 24 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
16449
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to write a safe but easy implementation (i.e. no codedom) for an IBindingListView.Filter (by compiling to a Predicate<T>). Anybody know if this is possible at all? Marc
1
1312
by: =?Utf-8?B?UGVkcm8=?= | last post by:
Hi I have VS .NET 2005 and I install LinQ preview for C#. I'm try to make some thing using LINQ. IT returns error in this sentence : var query = from p in people.People from s in people.Salaries where p.ID == s.ID)
8
46901
by: Andy | last post by:
Hi, I'm trying to add a where clause to my query: List<stringtypes = new List<string>(); types.Add( "A" ); types.Add( "B" ); query = query.Where( c =types.Contains( c.Type ) );
2
2538
by: Mucahit ikiz | last post by:
I cant make a full dynamic query in LINQ I have 2 situation methods (only_exp_query, only_tbl_query) those are working. .... using System.Linq.Dynamic; using System.Data.Linq; .... string connString = @"Data Source=.;Initial
0
2369
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's occuring enough to be a real cause for concern. The errors occur a couple times a week and the website is hit with constant traffic 24x7. Below are the two errors that are encountered. I had read something about MARS causing errors like this but...
14
3703
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
13
1815
by: Dan Tallent | last post by:
I have a query which I would like to convert to LINQ. I am having problems finding good examples or references that cover more complex queries. Here is the SQL command I would like to rewrite into C# LINQ. Notice that one of the returned fields "XrefID" is coming from the second table. This information is very important but its complicating the query expression in C# The DataContext has the two classes for the two tables...
2
2406
by: shapper | last post by:
Hello, I have the following Linq query: List<PostsTaginsert = (from t in (from t in database.Tags join p in paper.Tags on t.Name equals p.Name select t).ToList() select new PostsTag {
1
3361
by: jbot | last post by:
I've got some pages that use linq to sql. When I look at the generated sql statements in sql server profiler, I see the same statements repeated. In the example below (from a linq data source selected statement), the same sql query is run against the database 3 times at: oStudent.Count oStudent(0).lkpstatus.studentstatus oStudent(0).lkpstatus.statuscategory Is this the expected behavior? I expected the select to be run only
0
9877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10919
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5938
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.