i want this query to be converted in CodeIgniter format.
SELECT image.image_id, image.image_name, category_name,
image.image_description, image.image, image.status, image.cdate, image.user_id
FROM tbl_image as image
left join tbl_imagecat category on image.category_id=category.category_id
where ( 1 AND image.status = 0 AND image.user_id in (0,0)) ORDER BY image.image_id
$this->db->select("image.image_id, image.image_name, category_name, image.image_description, image.image, image.status, image.cdate, image.user_id");
$this->db->from("tbl_image as image");
$this->db->join("tbl_imagecat category", "image.category_id=category.category_id", "LEFT");
$this->db->where("image.status", "0");
$this->db->where_in("image.user_id", array("0") );
$this->db->order_by("image.image_id");
Note : I just convert only query, I don't concern with your logic.
Related
I am creating a result object from my query, something like this:
var result = from m in MyTable
join r in some_more_tables
select new ResultSummmary
{
Description = m.Description,
start_date = r.start_dat
};
But based on some condition before I get to this query and SELECT NEW, I want to be able to flex what I put in its Description filed, currently it is always m.Description but sometimes I want it to be a static text like "Hospital" and the rest of the times I want it to be m.Description as it is now.
How can we write it that way to be flexible?
Let's pretend the condition is stored in a variable called condition. This would allow you to write the following
var result = from m in MyTable
join r in some_more_tables
select new ResultSummmary
{
Description = condition ? m.Description : "Hospital",
start_date = r.start_dat
};
select new ResultSummmary
{
Description = someBool ? m.Description : "Hospital",
start_date = r.start_dat
};
When I output this code, I get { Price: 1446 } when I only want the number without the braces. Is there a way to do this? Also, once I get the price value, I want to convert it to a decimal. Is there a way to do this?
var flightPrice = (from f in XElement.Load(MapPath("flightdata.xml")).Elements("flight")
where (string)f.Element("flightnumber") == FlightNumber
&& (string)f.Element("destinationairportsymbol") == Destination
select new { Price = (Int32)f.Element("price") }
).SingleOrDefault();
lblPrice.Text = flightPrice.ToString();
Your select is creating an anonymous type with a single property: Price. If all you want is the actual price (and as a float), change your select to
// select new { Price = (Int32)f.Element("price") }
select (float)(int)f.Element("price")
However, it is not recommended that you use a float to deal with something financial like a price. The decimal data type is the preferred type for such a value.
select (decimal)f.Element("price")
What happens when you do:
lblPrice.Text = flightPrice.Price.ToString();
You can also do this:
// omited the first 3 lines of your linq statement...
select (Int32)f.Element("price")
).SingleOrDefault();
in which case flightPrice will be of type int.
Either put select (Int32)f.Element("price") instead of select new { Price = (Int32)f.Element("price") }
or
lblPrice.Text = flightPrice.Price.ToString();
Suppose my datatable is filled with data.
After filling data can we again put some condition on datatable with linq to extract data.
Suppose my datatable has 10 employee record.
So can we extract only those employee whose salary is greater than 5000 with linq query.
I know that we can achieve it datatable.select(). How can you achieve this with linq?
You can get a filtered set of rows, yes:
var query = table.AsEnumerable()
.Where(row => row.Field<decimal>("salary") > 5000m);
This uses the AsEnumerable and Field extension methods in DataTableExtensions and DataRowExtensions respectively.
Try this:
var query = (from t0 in dtDataTable.AsEnumerable()
where t0.Field<string>("FieldName") == Filter
select new
{
FieldName = t0.Field<string>("FieldName"),
FieldName2 = t0.Field<string>("FieldName2"),
});
SELECT t_PersonalInformation.personalInformation_Name, t_PersonalInformation.personalInformation_PresentAddress,
t_PersonalInformation.personalInformation_PermanentAddress, t_PersonalInformation.personalInformation_Phone,
t_PersonalInformation.personalInformation_Email,
t_Applicant.applicant_TotalExperience,
t_Experience.experience_CompanyName, CAST( t_Experience.experience_EndingYear AS INT) - CAST( t_Experience.experience_JoiningYear AS INT) AS yearOfExperience ,
t_Experience.experience_Responsibilities,
t_Training.training_TitleDetails, t_Training.training_Institute,
t_Training.training_Year, t_Training.training_Duration
FROM t_Applicant LEFT OUTER JOIN
t_PersonalInformation ON t_Applicant.applicant_user_ID = t_PersonalInformation.personalInformation_applicant_ID
LEFT OUTER JOIN
t_Experience ON t_Applicant.applicant_user_ID = t_Experience.experience_applicant_ID
LEFT OUTER JOIN
t_Training ON t_Applicant.applicant_user_ID = t_Training.training_applicant_ID
WHERE (t_Applicant.applicant_user_ID = 'hasib789')
i am using in C# with VS2008
I don't really have the time to go through all of that, but I can give you an idea of how it's done.
var query = from var applicant in t_Applicant
from perInf in t_PersonalInformation.Where(per => applicant.applicant_user_ID = per.personalInformation_applicant_ID).DefaultIfEmpty()
where applicant.applicant_user_ID == "hasib789"
select new ClassName{
Name = perInf!=null ? perInf.personalInformation_Name : <default value>,
PresentAddress = perInf!=null ? perInf.personalInformation_PresentAddress: <default value>,
...
etc
}
That second 'from' statement is one of the ways to do an outer join. That DefaultIfEmpty allows it to generate a new record of ClassName even if no matching record for perInf is found. Once you actually go to assign the values in your new record at the bottom, you have to check for null.
Using that template you should be able to get the rest of the query built.
I want to query a datatable (dt) and load a 2nd dt with the resultant collection of datarows. Fine - we have the CopyToDataTable() extn mthd for exactly that purpose. However it is constrained to enumerate only over DataRows, which means that I cannot return anything else e.g. a collection of anonymous types. So - how can I modify the values in the datarows?
Eg I have a dt with 3 columns: MyPK, VARCHAR01, VARCHAR02.
Foreach row, if VARCHAR01 or VARCHAR02 has the value "" (i.e. String.Empty) I want to replace that with NULL (which the underlying type allows).
I would do this as follows:
var MyCleanedDatarows =
from o in ds.Tables["dt"].AsEnumerable()
select new {
MyPK = o.Field<string>("MyPK"),
VARCHAR01 = (o.Field<string?>("VARCHAR01") == "" ? NULL : o.Field<string?>("VARCHAR01") ),
VARCHAR02 = (o.Field<string?>("VARCHAR02") == "" ? NULL : o.Field<string?>("VARCHAR02") )
};
...but then I cant use CopyToDataTable() to get back to a dt. I'm thinking I need to modify the datarows before invoking select operator, but I dont know how to achieve that. Any help/thoughts would be v.greatfully recieved.
Thanks in advance,
Tamim.
Take a look at this approach, in MSDN documentation.
http://msdn.microsoft.com/en-us/library/bb669096.aspx