LINQ how to translate subquery in FROM clause - c#

Newb to C#.Net / LINQ
I have to convert some dynamic stored procs to LINQ. I'm new to LINQ, and looking for an example to follow. I have something like the following:
sql1 VARCHAR2(32000) := ' SELECT a,b,c FROM ( ';
from1 VARCHAR2(32000) := ' SELECT x,y,z FROM t1, t2,
(SELECT xx FROM aTable WHERE foo = 'bar' )
WHERE x=1
AND y=2';
Anyway, hope this gets the example across.
I was hoping to do something like:
var subSelect1 = (from val1 in aTable
where val1.foo = "bar"
select new {
val1.foobar
}).AsEnumerable();
var mainSelect = (from <how do I use the subSelect1 as a virtual table here?>
Is this possible in LINQ?
The actual query is quite long and complex...having several subqueries in the FROM clause of the main select, and some of the subqueries in the FROM have themselves nested subqueries in -their- FROM clause.

What may help is going through a couple sample linq queries and look at the results. For example, the following code will create an IEnumerable of val1's.
var subSelect1 = (from val1 in aTable
where val1.foo = "bar"
);
Note the above is equivalent to the below.
var subSelect1 = (from val1 in aTable
where val1.foo = "bar"
select val1 /* this select statement is implied in the above */
);
Adding the select new {val1.foobar} after the where clause creates an IEnumerable of an anonymous type, with one property named foobar. This means that you'll only be able to join against the one property foobar.
var subSelect1 = (from val1 in aTable
where val1.foo == "bar"
select new {val1.foobar}
);
var mainSelect = (from f in subSelect1
where f.foobar == "test")
By leaving out the select new, you'll have access to all the fields in val1.
var subSelect1 = (from val1 in aTable
where val1.foo == "bar"
);
var mainSelect = (from v in subSelect1
where v.foobar == "test"
and v.bar == "status"
)

I am not very clear regarding your exact requirement, but my understanding is you need a chained Linq Query such that the parent data can be filtered using the Subquery result. Also the Fluent syntax is a set of extension methods on IEnumerable<T>, therefore a DataTable needs AsEnumerable() to be called so that operations can be done on IEnumerable<DataRow> and later on CopyToDataTable in the System.Data.DataSetExtensions can be called for re-conversion. Also since we are dealing with DataRows, therefore value access need indexer, like dataRow["ColumnName"] or dataRow[ColumnIndex]
// dr is the DataRow type (fetching all the Datarows, instead of specific Column like foobar)
Creating Subselect1 IEnumerable<DataRow>
var subSelect1 = aTable.AsEnumerable()
.Where(dr => dr["foo"] == "bar")
var mainSelect = (from <how do I use the subSelect1 as a virtual table here?
This shall be simple, depending on exactly what you need to do I assume, you need to match the DataRows that exist in subSelect1, then so something like:
var mainSelect = MainDataTable.AsEnumerable()
.Where(dr => subSelect1.Contains(dr))
var mainSelectDataTable = mainSelect.CopyToDataTable();
Idea remains, its very simple to create complex chained queries in Linq, provided you know exactly what you need, which is not very clear from the question

Related

How to do a LINQ query with Split operation

I have the following linq query:
MyClass myobj = (from p in Session.All<MyClass>()
where p.tags.Split(' ').Contains(searchTag)
select p).FirstOrDefault();
When I run this, I get:
System.NotSupportedException: LINQ to Entities does not recognize the
method 'System.String[] Split(Char[])' method, and this method cannot
be translated into a store expression.
What is a good way to do what I am attempting to do?
The real problem here is the db design, but assuming you have no control over this one idea is to split the query.
First retrieve any rows that contain the search tag anywhere within them.
List<MyClass> myobjs = (from p in Session.All<MyClass>()
where p.tags.Contains(searchTag)
select p).ToList();
Then perform the correct tag search on the retrieved objects in memory.
MyClass myobj = myobjs.FirstOrDefault(m => m.tags.Split(' ').Contains(searchTag));
So, assuming tags = tag1 tag2 tag3, and searchTag is tag2 you can just do:
MyClass myobj = (from p in Session.All<MyClass>()
where p.tags.IndexOf(searchTag) > -1
and searchTag.IndexOf(" ") == -1
select p).FirstOrDefault();

Switch gears from TSQL to LINQ

I am trying to take the TSQL below and convert it into LINQ. I'm obviously not very good at this and not sure how to go about doing a join in Linq. My confusion goes a little beyond just the simple expression. In my basic uses my result set is a class that of course represents a table row...but a join would not have this one to one ratio so do I have to create a custom result set?
TSQL:
SELECT
SimEA.*,
SimE.*
FROM
dbo.SSimEmailsAdressees SimEA
JOIN dbo.SSimEmails SimE ON
SimEA.EmailID = SimE.EmailMsgID
WHERE
SimEA.UserId = var
The closest I have come:
this.GetAll<SSimEmails>()
.Where(e => e.SSimEmailsAdressees.Any(p => p.UserId.ToString() == usrID));
Which of course return an object that is a mimic of the single table being queried. So I need help getting the join added and I'm guessing I'll have to create a return object something like this?
this.GetAll<MyJoinResultsObject>......
I think that you should write something like:
var q =
from SimEA in SSimEmailsAdressees
where SimEA.UserId = usrID
join SimE in SSimEmails on SimEA.EmailID equals SimE.EmailMsgID
into joinSimE
from SimE in joinSimE.DefaultIfEmpty()
select new { EmailID = SimEA.EMailID, UserId = SimEA.UserId, OtherField = SimE.OtherField };
var result = from ea in SimEA
join e in SimE
on ea.EmailID equals e.EmailMsgID
where ea.UserId = userId
select new { properties you want here or new object based on properties}

How can i add a condition string to an sql query in an var type

Im working on an source code with an sql query in a VAR type like
var query = select ... from ... where ... ;
is it possible to add an dynamic "where clause" like
string condition = "where x.x > x.y";
e.g. var query = select ... from ... + condition;
Iam sorry for my bad english
You are not clearly stating how your query looks like. Is it a result of a LINQ operation or simply a String?
The keyword var is only usable for design time. The compiler will substitute it with the correct datatype.
If you SQL query is a string, like
var query = "Select ... from ... where ..";
then
string condition = "where x.x > x.y";
query += condition;
is valid because both variables are strings. You can't combine a non string type with a string the way your code suggests.
I do now assume that you are using a LINQ syntax. It is possible to add such conditions to a linq query per code, I think the keywords linq query builder, expression tree and predicate should get you started.
I'd strongly suggest that you stop using the var keyword without exactly knowing what it does and where to use it.
Dynamic Linq exists specifically to solve late-bound scenarios for LINQ:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Allows constructs such as:
NorthwindDataContext northwind = new NorthwindDataContext();
var query = northwind.Products
.Where("CategoryID = 3 AND UnitPrice > 3")
.OrderBy("SupplierID");
If you do not call ToList() and your final mapping to the DTO type, you can add Where clauses as you go, and build the results at the end:
var query = from u in DataContext.Users
where u.Division == strUserDiv
&& u.Age > 18
&& u.Height > strHeightinFeet
select u;
if (useAge)
query = query.Where(u => u.Age > age);
if (useHeight)
query = query.Where(u => u.Height > strHeightinFeet);
// Build the results at the end
var results = query.Select(u => new DTO_UserMaster
{
Prop1 = u.Name,
}).ToList();
This will still only result in a single call to the database, which will be effectively just as efficient as writing the query in one pass.
I saw this answer here by Reed Copsey

How do I implement a dynamic 'where' clause in LINQ?

I want to have a dynamic where condition.
In the following example:
var opportunites = from opp in oppDC.Opportunities
join org in oppDC.Organizations
on opp.OrganizationID equals org.OrgnizationID
where opp.Title.StartsWith(title)
select new
{
opp.OpportunityID,
opp.Title,
opp.PostedBy,
opp.Address1,
opp.CreatedDate,
org.OrganizationName
};
Some times I have Title and sometimes I don't. And also I want to add date in where clause dynamically.
For example, like this SQL:
string whereClause;
string SQL = whereClause == string.Empty ?
"Select * from someTable" : "Select * from someTable" + whereclause
You can rewrite it like this:
var opportunites = from opp in oppDC.Opportunities
join org in oppDC.Organizations on opp.OrganizationID equals org.OrgnizationID
select new
{
opp.OpportunityID,
opp.Title,
opp.PostedBy,
opp.Address1,
opp.CreatedDate,
org.OrganizationName
};
if(condition)
{
opportunites = opportunites.Where(opp => opp.Title.StartsWith(title));
}
EDIT: To answer your question in the comments, yes, you can keep appending to the original Queryable. Remember, this is all lazily executed, so at this point all it's doing it building up the IQueryable so you can keep chaining them together as needed:
if(!String.IsNullOrEmpty(title))
{
opportunites = opportunites.Where(.....);
}
if(!String.IsNullOrEmpty(name))
{
opportunites = opportunites.Where(.....);
}
You can dynamically add a where clause to your IQueryable expression like this:
var finalQuery = opportunities.Where( x => x.Title == title );
and for the date similarly.
However, you will have to wait to create your anonymous type until after you've finished dynamically added your where clauses if your anonymous type doesn't contain the fields you want to query for in your where clause.
So you might have something that looks like this:
var opportunities = from opp in oppDC.Opportunities
join org in oppDC.Organizations on
opp.OrganizationID equals org.OrgnizationID
select opp
if(!String.IsNullOrEmpty(title))
{
opportunities = opportunities.Where(opp => opp.Title == title);
}
//do the same thing for the date
opportunities = from opp in opportunities
select new
{
opp.OpportunityID,
opp.Title,
opp.PostedBy,
opp.Address1,
opp.CreatedDate,
org.OrganizationName
};
The WHERE clause could be done something like
//...
where string.IsNullOrEmpty(title) ? true : opp.Title.StartsWith(title)
//...
Dynamically returning records I don't think is possible in LINQ since it needs to be able to create a consistent AnonymousType (in the background)
Because queries are composable, you can just build the query in steps.
var query = table.Selec(row => row.Foo);
if (someCondition)
{
query = query.Where(item => anotherCondition(item));
}
If you know in advance all possible where queries like in the SQL example you have given you can write the query like this
from item in Items
where param == null ? true : ni.Prop == param
select item;
if you don't know all possible where clauses in advance you can add where dymically for example like this:
query = query.Where(item => item.ID != param);
The following questions and answers address this quite well:
Dynamic where clause in LINQ - with column names available at runtime
Is there a pattern using Linq to dynamically create a filter?
I was searching for creating a dynamic where clause in LINQ and came across a very beautifull solution on the web which uses ExpressionBuilder in C#.
I am posting it here since none of the above solution uses this approach. It helped me. Hope it helps you too
http://www.codeproject.com/Tips/582450/Build-Where-Clause-Dynamically-in-Linq

LINQ to Entity: using Contains in the "select" portion throws unexpected error

I've got a LINQ query going against an Entity Framework object. Here's a summary of the query:
//a list of my allies
List<int> allianceMembers = new List<int>() { 1,5,10 };
//query for fleets in my area, including any allies (and mark them as such)
var fleets = from af in FleetSource
select new Fleet
{
fleetID = af.fleetID,
fleetName = af.fleetName,
isAllied = (allianceMembers.Contains(af.userID) ? true : false)
};
Basically, what I'm doing is getting a set of fleets. The allianceMembers list contains INTs of all users who are allied with me. I want to set isAllied = true if the fleet's owner is part of that list, and false otherwise.
When I do this, I am seeing an exception: "LINQ to Entities does not recognize the method 'Boolean Contains(Int32)' method"
I can understand getting this error if I had used the contains in the where portion of the query, but why would I get it in the select? By this point I would assume the query would have executed and returned the results. This little ditty of code does nothing to constrain my data at all.
Any tips on how else I can accomplish what I need to with setting the isAllied flag?
Thanks
This poached from a previous answer...
Contains not supported.
IN and JOIN are not the same operator (Filtering by IN never changes the cardinality of the query).
Instead of doing it that way use the join method. It's somewhat difficult to understand without using the query operators, but once you get it, you've got it.
var foo =
model.entitySet.Join( //Start the join
values, //Join to the list of strings
e => e.Name, // on entity.Name
value => value, //equal to the string
(ModelItem ent, String str) => ent);//select the entity
Here it is using the query operators
var foo = from e in model.entitySet
join val in values on
e.Name equals val
select e;
Basically the entity framework attempts to translate your LINQ query into a SQL statement but doesn't know how to handle the Contains.
What you can do instead is retrieve your fleets from the database and set the isAllied property later:
var fleets = (from af in FleetSource
select new Fleet
{
fleetID = af.fleetID,
fleetName = af.fleetName,
userId = af.userId
}).AsEnumerable();
foreach (var fleet in fleets)
{
fleet.isAllied = (allianceMembers.Contains(fleet.userID) ? true : false);
}
Everyone above me is wrong!!! (No offense ...) It doesn't work because you are using the IList overload of "Contains" and not the IEnumerable overload of "Contains". Simply change to:
allianceMembers.Contains<int>(af.userID)
By adding the <int>, you are telling the compiler to use the IEnumerable overload instead of the IList overload.
var fleets = from af in FleetSource;
var x = from u in fleets.ToList()
select new Fleet
{
fleetID = u.fleetID,
fleetName = u.fleetName,
isAllied = (allianceMembers.Contains(u.userID) ? true : false)
}
calling ToList() on fleets the query is executed, later you can use Contains().

Categories