We are using below code for dynamic groupjoin on SecurityID integer Column
but it includes the resultset with with nullable values.
var innerJoin = source.AsQueryable().GroupJoin(destination.AsQueryable(),
"new (outer.SecurityId as SecurityId)",
"new (inner.SecurityId as SecurityId)",
"new (outer as sources, group as destinations)").
SelectMany("destinations", "new(outer.sources as sources, inner as destinations)");
We want to avoid null values from the above query. The above query performs innerjoin and is working fine if we have joins on string columns.
Please help us to achive the above query to perform inner join on integer columns which may have 'null' values.
First of all, you can not assign null to int, instead you need to use Nullable<int> / int?. If that's the case then just filter your source using Where clause and then perform GroupJoin. I think that is the easiest way.
Hi I need some help coverting this sql statement to Linq, I am very new to Linq and LinqToSql, and this is my weakness, it seems like this is used frequently and I need to wrap my brain around the syntax. The Code is below.
select distinct t1.Color from [ProductAttributes] t1 join [Product] t2 on t1.Name = t2.ProductName where t1.ProductID = #productID order by t1.color
#productID is the parameter coming into the function, where I am trying to use Linq in MVC.
Thanks
It might be like this I guess
int myProductID = 1;//or whatever id you want.
MyDataContext mdc = new MyDataContext(CONNECTION_STRING_IF_NEEDED);
//MyDataContext is your datacontext generated by LinqToSql
var result = (from x in mdc.ProductAttributes
join y in Products on x.Name.equals(y.ProductName)
where x.ProductID = myProductID
orderby x.color
select x.Color).Distinct();
Note That Table names might need to be fixed.
Let say I have the following sql table. Customer_id column is a primary key with smallint.
And I am trying to create a linq query, where I want to get all data for customer with id, let say 1.
How to do this.
Already done and not working:
1.
var query = from row in dt.AsEnumerable()
where row.Field<Int32>("customer_id") == Convert.ToInt32(2)
select row;
2.
var query = from row in dt.AsEnumerable()
where row.Field<Int16>("customer_id") == Convert.ToInt16(2)
select row
debug for example 1,2
Syntax error
Exceptions
Why don't you use this:
DataRow needle = hayStack.Tables["Customer"].Rows.Find(2);
Your method should be rewritten as something like this:
private DataRow GetCustomerDetails(Int16 customer_id)
{
return _dt.Tables["Customer"].Rows.Find(customer_id);
}
The calling method would have to check for null beeing returned from the method, since an invalid customer_id would cause Find() tu return null.
Try using short type instead of Int32.
I have a collection of IDs in memory, and I would like to fetch only rows from a DB matching those IDs.
In SQL, I could either write a query like SELECT * FROM mytable WHERE id IN (1,3,5,10) or do a join between tables.
My problem is that EF can't build a query where I join my EF-data with my local array or list.
(I'm using EF4.1, but I'm guessing the problem/solution would be similar in older versions, as well as with Linq-to-SQL.)
You can use Contains() with your ID collection myIDs to generate the equivalent WHERE id IN .. query:
var results = context.mytable.Where(x => myIds.Contains(x.Id));
I have no problem with SQL, but I'm finding Linq a little confusing.
C#, .NET4, Silverlight, RIA services, Oracle DB (v?), VS2010 running Devart dotConnect 6.10.121.
I have a RIA Entity
public sealed partial class ProcessLogHdr : Entity
{
DateTime JobDate;
string InterfaceName;
int SuccessfulCount;
int FailCount;
int TotalCount;
}
There are more fields such as user, etc, that won't be applicable to this post.
There are many jobs that make up a process. Each job has an entry in this table, but the view I want is a date group by summary.
I will be calling context.Load on a query, where I pass in the start and end date, which in Oracle looks like this:
select
trunc(JobDate),
InterfaceName,
sum(SuccessfulCount) as Total_Pass,
sum(FailCount) as Total_Fail,
sum(TotalCount) as Total,
max(JobDate) as Last_Msg_Processed_At_DT
from
ProcessLogHdrsEntity
where
JobDate >= START_DATE_IN_VAR and
JobDate <= END_DATE_IN_VAR
group by
trunc(JobDate),
InterfaceName
order by
trunc(JobDate) desc,
InterfaceName asc;
conttext.Load will call the linq query from a method that returns IQueryable.
The linq statement must run for Oracle under Devart dotConnect for Oracle.
I'm guessing I need some custom class to hold the results, like ProcessLogHdrDateSummary.
If you guys could help me fill in the missing ????? linq, I would be so grateful:
public IQueryable<ProcessLogHdrDateSummary> GetProcessLogHdrsDateSummary(DateTime START_DATE_IN_VAR, DateTime END_DATE_IN_VAR)
{
return ?????
}
Many Thanks!
There's no simple answer to this. One of the characteristics of LINQ to database providers is that some queries execute immediately, while others don't. Aggregation functions (MAX, MIN, etc) return immediately. So do some LINQ functions that specify particular output, such as .First(). Anything returning a collection will likely not execute immediately, and will return an IQueryable<> of some type.
What type? That depends on what the select clause of the LINQ statement specifies (which is not the same as the generated SQL select clause). "from c in db.customers select c" returns customer objects, but you can also use the select clause to populate other classes, or anonymous classes.
If a LINQ query returns an IQueryable<>, remember that the query hasn't executed yet! It won't execute until you start processing the data. You must process the data while still in scope of the data context, because once that's gone, you've lost your database connection.
You can always force an IQueryable<> to execute by ending it with .ToList(), .ToArray(), .ToDictionary(), or a few others. The List<> will use the same generic type as the IQueryable<> and the select clause (or .Select() method) of the LINQ statement.
The LINQ query will be rather complicated. I recommend that you follow these steps:
1. Create a stored procedure with an out cursor:
CREATE PROCEDURE myQuery(
DATE START_DATE_IN_VAR,
DATE END_DATE_IN_VAR,
cur out sys_refcursor) AS
BEGIN
OPEN cur FOR SELECT
trunc(JobDate),
InterfaceName,
sum(SuccessfulCount) as Total_Pass,
sum(FailCount) as Total_Fail,
sum(TotalCount) as Total,
max(JobDate) as Last_Msg_Processed_At_DT
from
ProcessLogHdrsEntity
where
JobDate >= START_DATE_IN_VAR and JobDate <= END_DATE_IN_VAR
group by
trunc(JobDate), InterfaceName
order by
trunc(JobDate) desc, InterfaceName asc;
END;
2. Add this procedure to the model. If you are using Devart Entity model, the return type will probably be generated. In other case you will need to create an entity or a complex type that will represent the return type of the generated method.
3. Treat the method call as a usual DomainService method.