I want to do a LINQ query and add in a column that is null. Something like this:
var query = (from all in data
select new
{
all.Column1,
all.Column2,
all.Column3,
newColumn = null
})
However, this gives me the error "cannot assign null to anonymous type property".
Add the null with appropriate cast depending on the type of newColumn such as e.g. newColumn = (string) null;
Related
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.
In my sql query I'm having this where condition:
WHERE
parentID = prj.parent_id
and mandator_fk = mandator_id
The variable parentID is null as default.
If this variable is null the WHERE condition should be parentID is null and not like in the query above (parentID = prj.parent_id). If this variable is filled the WHERE condition should be parentID = prj.parent_id.
How can I perform this with only one sql query?
EDIT: its inside a function, thats why I would have only one sql query for this.
This is not working I know that, but this is like what I need:
WHERE
CASE
WHEN parentID is null THEN parentID is null
ELSE
parentID = prj.parent_ID
END
and mandator_fk = mandator_id
TIA
frgtv10
Seems you want to join on nulls, so you'll need to convert them first. Try this:
where
NVL(parentId, 0) = NVL(prj.parent_id, 0)
and ...
I'm not sure if NVL() function is surgable, so here is another way to write the condition:
WHERE
( parentID = prj.parent_id
OR parentID IS NULL AND prj.parent_id IS NULL
)
AND mandator_fk = mandator_id
I am trying to get customerId from Customer table.
However, when I try to make it, it has error said, 'Cannot implicitly convert type 'System.Linq.Iqueryable' to 'int''
How can I get customerId??
Thank you.
Here is my coding.
int customerId = from a in db.Customer
where a.userName == customerName
select a.customerId;
//it has error in here.
order.customerId = customerId;
It's possible that your query could return multiple rows. You need to tell LINQ that you only want the first (or a single) result:
int customerId = (from a in db.Customer
where a.userName == customerName
select a.customerId).First();
I like using the .Single() function when I know there should be only one row.
int customerId = (from a in db.Customer
where a.userName == customerName
select a.customerId).Single();
This throws an exception if more or less than one row is found, which is sometimes useful depending on your situation.
use it this way:
var customer = db.Customer.FirstOrDefault(c =>c.userName == customerName)
var id = customer.Id
Select returns an IEnumerable, so you can't convert it to int.
There is one more suggestion I can recommend for this question is to use First or default.
var customerId = (from a in db.Customer
where a.userName == customerName
select a.customerId).FirstOrDefault();
order.customerId = customerId; this should now work fine since it already knows it int
I want to return only one column value and other column values as null from database. how can i do that in LINQ? also have to make values of not nullable fields to null while retrieving
C#
var results = from item in db.items
select (item.col1 == value ? item.col1 : null);
VB.NET
Dim results = From item in db.items
Select iif(item.col1 == value ? item1.col : Nothing)
select ID, 0 as Index from Table;
How do I do this query in linq?
var o = From X in Table Select X.ID, (0 as Index) <- error
var o = from x in Table select new { ID = X.ID, Index = 0 };
The new keyword here creates a new anonymous type that gets exposed by the query. The first property, ID, is populated by the ID property from X. The second property, Index, is given the static value of 0.