im trying to do something like this
var names_en = dtTmp.AsEnumerable();
var names = names_en.Where(a => a["ID"] == "1");
string name = names["Name"].ToString();
My goal is to find the row where ID equalls 1 and keep the Name as string
I know I have ID collumn I know that I have a row where ID==1 I see it in the debugger.
but names lets that enumeration yields no results.
also is there a better way to do this?
Its better if you use DataRowExtension.Field method which is strongly typed. Also make sure you ID is of type string, otherwise you will get an exception. You may specify the type accordingly.
string name = dtTmp.AsEnumerable()
.FirstOrDefault(r => r.Field<string>("ID") == "1")
.Field<string>("Name");
var names = names_en.Where(a => a["ID"] == "1");
return the List of result you must get the first record
Related
I have a List containing a number of Guid's.
List<Guid> recordIds = new List<Guid>;
I need to verify if the Guid's in this list are all identical.
So instead of iterating the whole list I was thinking about using some sort of:
var IdsAreIdentical = recordIds.TrueForAll(x => x == DontKnowWhatToPutHere);
My issue is that I am not really shure about the usage. Maybe somebody can put me in the right direction.
If you want to verify if all the id are the same, you could verify that all the values are the same as the first one :
bool allIdentical = recordIds.TrueForAll(i => i.Equals(recordIds.FirstOrDefault());
Another variant would be to verify the number of distinct values that you have. If the result is 1, the ids are all identicals.
var allIdentical = list.Distinct().Count() == 1;
I'm using the last driver. My documents are of the form
{
"ItemID": 292823,
....
}
First problem: I'm attempting to get a list of all the ItemIDs, and then sort them. However, my search is just pulling back all the _id, and none of the ItemIDs. What am I doing wrong?
var f = Builders<BsonDocument>.Filter.Empty;
var p = Builders<BsonDocument>.Projection.Include(x => x["ItemID"]);
var found= collection.Find(f).Project<BsonDocument>(p).ToList().ToArray();
When I attempt to query the output, I get the following.
found[0].ToJson()
"{ \"_id\" : ObjectId(\"56fc4bd9ea834d0e2c23a4f7\") }"
It's missing ItemID, and just has the mongo id.
Solution: I messed up the case. It's itemID, not ItemID. I'm still having trouble with the sorting.
Second problem: I tried changing the second line to have x["ItemID"].AsInt32, but then I got an InvalidOperationException with the error
Rewriting child expression from type 'System.Int32' to type
'MongoDB.Bson.BsonValue' is not allowed, because it would change the
meaning of the operation. If this is intentional, override
'VisitUnary' and change it to allow this rewrite.
I want them as ints so that I can add a sort to the query. My sort was the following:
var s = Builders<BsonDocument>.Sort.Ascending(x => x);
var found= collection.Find(f).Project<BsonDocument>(p).Sort(s).ToList().ToArray();
Would this be the correct way to sort it?
Found the solution.
//Get all documents
var f = Builders<BsonDocument>.Filter.Empty;
//Just pull itemID
var p = Builders<BsonDocument>.Projection.Include(x => x["itemID"]);
//Sort ascending by itemID
var s = Builders<BsonDocument>.Sort.Ascending("itemID");
//Apply the builders, and then use the Select method to pull up the itemID's as ints
var found = collection.Find(f)
.Project<BsonDocument>(p)
.Sort(s)
.ToList()
.Select(x=>x["itemID"].AsInt32)
.ToArray();
I have 3 tables namely Ship[ShipID, Name, YearOfConstr, CompanyID(FK), TypeID(FK)] which is a bridge between Company[CompanyID, Name, Headquarter] and Type[TypeID, Description, NoPassengers]
I wanted to query the names of all Company which has a specific type = "xxx" and whose headquater = "yyy"
Below is what I have tried, but it's returning nothing and won't throw an error either.
public List<string> AllShippingCompanies(string TypeDescription, string headquarters)
{
var list = from shipcomany in dbContext.Ships.Where(x => x.Type.Description == TypeDescription && x.ShippingCompany.Headquarter == headquarters)
select shipcomany.ShippingCompany.Name;
return list.ToList();
}
What could I have been doing wrong ?
I just check and found there are no related data in my DB. The code works fine. It's correct. Thanks for your time
I have made an method that will check if name value contains in the name column on current table, but I also need to see if the name contains in another column that is in a many table(TABLE.Project).
TABLE.Customer 1 --- *(many) TABLE.Project(Which have column named "Name")
This is the method:
Public List<SearchObject> Finditem(string name)
{
var query = from o in db.tbl_Custommer
where o.Name.Contains(name)
select new SearchObject
{
Url = o.tbl_Webs.WebUrlName,
Name = o.Name,
};
return query.ToList();
}
Do I need to iterate throught each o.Project?
Any kind of help is appreciated alot!
presuming you have the relationship correctly setup and its called Projects then you could use any - i.e. return customers where the name matches name and they have at least one project with the name also matching:
var query = from o in db.tbl_Custommer
where o.Name.Contains(name) && o.Projects.Any(p => p.Name.Contains(name))
select new SearchObject
{
Url = o.tbl_Webs.WebUrlName,
Name = o.Name,
};
I am trying to sort some data from entity before passing them to another function. Both tableName and columnName are selected dynamically. This is what I'm doing:
string tableName = cboSelectTable.Text.ToString();
var comboBoxColumn = from a in Context.RULES where
a.ISCOMBOBOXCOLUMN == "Y" select a.COLUMN;
foreach (var colName in comboBoxColumn)
{
var getTableName = entityModel.GetType()
.GetProperty(tableName)
.GetValue(entityModel, null);
var getData = ((IQueryable<object>)getTableName)
.Where(colName + "!=null")
.Select(colName)
.Distinct()
.OrderBy('"'+ colName +'"');
dgvCboColumn(getData, colName);
}
But this is not sorting the data. I also tried some other methods too, but couldn't figure it out. How can I resolve this?
I do not think that is the way, .OrderBy(...) works. Try ordering by an attribute:
SomeList.OrderBy(l => l.SomeAttribute);
I suspect ordering by a string results in each element being ordered by the same attribute, thus not ordering at all. In addition, I am not sure, if your where-clause works like that.
There is another relevant SO question already answered. You might want to have a look at it: click me!
You can simply place the Select after the OrderBy:
var getData = ((IQueryable<object>)getTableName)
.Where(colName + "!=null")
.OrderBy(colName)
.Select(colName)
.Distinct();
This will allow you to reference the column name. (doesn't seem to work for some reason).
Alternatively, you can reference the current instance:
var getData = ((IQueryable<object>)getTableName)
.Where(colName + "!=null")
.Select(colName)
.Distinct()
.OrderBy("it");