Good Morning,
I wonder if there's any method within the NHIBERNATE to which I can retrieve the first row of the table?
For example:
Line | ID | Name |Last Name |
1 | 0 | Test | of Information |
2 | 1 | Mauricio | Silva |
If I want the first line or the line 1 of the table
You can use Linq to create queries with nHibernate. There is a method called FirstOrDefault() which takes only the first record. If the query return empty, the FirstOrDefault method will return null, so, remember to check the result before using, for sample:
var firstItem = session.Query<Entity>().FirstOrDefault();
if (firstItem != null)
{
string name = firstItem.Name;
// use object
}
NHibernate does support paging, so we can select "any" record using the .Take() and .Skip(). In our case we can do it like this:
var list = session
.QueryOver<Person>()
.Take(1) // this will take just a first record
//.Skip(0) // we can even skip some, to get next page
;
Then the resulting list will contain 1 or none row...
var person = list.FirstOrDefault();
Also, we can never be sure, what order will be used by DB engine, so we should use explicit ORDER BY:
var list = session
.QueryOver<Contact>()
.OrderBy(p => p.ID)
.Asc
.Take(1)
;
Now we can be sure, that the first result will be with ID == 0
Related
Here is the table setup I have:
int | Design_1
0 | Design_A
1 | Design_B
2 | Design_C
Here is my code for the form:
var design = (from d in BikePartsDataContext1.handlebars
where d.#int == "0"
select d.Design_1);
this.textBox1.Text = design.ToString();
What I am trying to do is make the textBox1 text have the value of the Design_1 value from the row where #int is 0.
All works fine until I get this as the text value for textBox1:
SELECT [t0].[Design 1] FROM [dbo].[handlebars] AS [t0] WHERE [t0].[int] = #p0
I think you want the first record then based on Id?
// at top of file so you can use the extension methods
using System.Linq;
// code
var design = (from d in BikePartsDataContext1.handlebars
where d.#int == 0 // i removed the quotes add them back if this is truely a string/sql varchar
select d.Design_1).Single(); // use single to ensure only 1 record will get selected
this.textBox1.Text = design; // design will now be the value of Design_1
Some notes:
Single will throw an exception if no records are found OR if more than one record is found.
If there can be 0 records use SingleOrDefault
If there can be more than 1 record and you do not care which one is used then use First or FirstOrDefault
I assumed your id was an int and not a string so I removed the quotes around 0, add them back if this is not the case
You can also rewrite it using only lambda expressions:
this.textBox1.Text = BikePartsDataContext1.handlebars
.Where(x => x.#int == 0)
.Select(x => x.Design_1)
.Single();
This takes a little bit of setup to explain. I know "nulls are bad" in a database and I am experiencing why that can be so, but I have no choice about the way the business uses the data source. The data source has nulls in the dataset and I have to work with it as-is. (Hopefully this addresses the anticipated "Well, your dataset shouldn't have nulls..." or "Why don't you just remove the nulls...?")
Suppose I have sample set like this, where "NULL" is an actual null:
Campus | Name | Relationship
---------------------------------
A | Bob | Relationship 1
B | Bill | NULL
B | Carol | Relationship 2
C | Sally | Relationship 1
Now suppose I am using an option list to filter the results by a distinct list of the values in the Relationship column:
All (meaning show all records)
NULL
Relationship 1
Relationship 2
If I didn't have the "All" option, it would be simple enough:
private IEnumerable<RwsBasePhonesAndAddress> PopulateQuery(string SelectedCampus,
string SelectedRelationship)
{
IEnumerable<RwsBasePhonesAndAddress> query =
db.RwsBasePhonesAndAddresses.Where(m => m.Campus == SelectedCampus);
if (!string.IsNullOrEmpty(SelectedRelationship))
query = query.Where(m => m.Relationship == SelectedRelationship);
else
query = query.Where(m => m.Relationship == null);
query = query.OrderBy(m => m.StudentName).AsEnumerable();
return query;
}
I have verified that the code as written returns results matching the "SelectedRelationship" parameter, including "NULL" records when "NULL" is chosen as a filter.
It's the inclusion of the "All" in the option list that makes this difficult. If the "Relationship" column had no nulls, I could use a null SelectedRelationship parameter as the "do not filter by 'SelectedRelationship'" option.
Is there some way that I can use a "do not filter" option into the code?
Not completely sure that I understood the question. From what I understood to the code above you are missing the part that if you get an "All" value then not to filter out anything. Right? If so:
private IEnumerable<RwsBasePhonesAndAddress> PopulateQuery(string SelectedCampus,
string SelectedRelationship)
{
IEnumerable<RwsBasePhonesAndAddress> query =
db.RwsBasePhonesAndAddresses.Where(m => m.Campus == SelectedCampus);
if(string.IsNullOrEmpty(SelectedRelationship))
query = query.Where(m => m.Relationship == null);
else if (SelectedRelationship != "All")
query = query.Where(m => m.Relationship == SelectedRelationship);
query = query.OrderBy(m => m.StudentName).AsEnumerable();
return query;
}
how can I use link to fetch one-to-one relation that does not contain duplicates? Example:
ID | STATUS
1 | CHECKIN
2 | CHECKOUT
2 | CHECKOUT
1 | CHECKIN
3 | CHECKOUT <--
I should only retrieve the ID 3 CHECKOUT because he is not duplicated.
Can you help me using linq?
You need to make a Group and ask for only group items that = 1
Dim nonDuplicates = (From x In query Group By x.Id, x.Status Into grp = Group
Where grp.Count = 1)
The other answer will still retrieve all the duplicated items, just removing duplicates of them. If you want to only retrieve non-duplicated items, as you stated in your original question, this will work for you:
Item singles = items.Where(i => !items.Any(j => !i.Equals(j) && i.id == j.id));
I have a schedule object that is returned from the database. It contains information from a few tables. One of the tables is called ScheduleData and has four columns. It has this format:
Id | ScheduleId | Name | Value
I need the value of the fourth column where the Name is Mine and the ScheduleId is 5
I have tried this, but it doesn't work:
string val = from s in schedule.ScheduleData where s.Name.Equals("Mine") && s.ScheduleId == 5 select s.Value;
Use First method or FirstOrDefault method.The query returns an IEnumerable<T>, you can't assign it to string.
string val = (from s in schedule.ScheduleData
where s.Name == "Mine" && s.ScheduleId == 5
select s.Value).First();
I have a datatable like:
ID | ID2
--------
1 | 2
1 | 3
12 | 2
15 | 3
I need to filter data table with ID2(I know two ID2 values in this case 2 and 3) in such a way that i should get output as
ID | ID2
--------
1 | 2
1 | 3
That means ID which have two ID2(2 and 3) should be selected.
dt.Select("ID2=2 AND ID2=3"); is not working in this case.
Thanks
It is not clear what are you searching for.
If you want to extract all the rows in which the same value for the ID column appears two or more times then
DataRow[] rows = dt.Select("Count(ID) > 1)")
You were right, this doesn't work on a datatable without relationships.
I have found a solution usig Linq,
// Returns an IGrouping<int,DataRow> for every ID that appears more than one time
var result = dt.AsEnumerable().GroupBy(z => z.Field<int>("ID"))
.Select(q=> new{Count = q.Count(), ID=q.Key})
.Where(x => x.Count > 1);
// Now we could extract the rows in the usual way
foreach(var l in result)
{
DataRow[] r = dt.Select("ID=" + l.ID);
r.Dump();
}
I don't know how efficient is this, but at least this seems to work
You can use LINQ to DataSet
var rows = table.AsEnumerable().Where(r => r.Field<int>("ID") == 1);
BTW according to desired result you should filter by ID.