IUpdatable<T> and IValueInsertable<T> from optional values? - linq2db

Is it possible to build something like this in Linq2DB? Currently I have a problem that ITable<T> is not assignable to either IUpdatable<T> or IValueInsertable<T>. Or maybe what I'm trying to achieve is not a good practice?
var optional1 = Optional<int>.None();
var optional2 = Optional<int>.Of(123);
var table = Database.MyTable; // ITable<MyRecord>
if (optional1.HasValue) table = table.Set(x => x.Optional1, optional1.Value);
if (optional2.HasValue) table = table.Set(x => x.Optional2, optional2.Value);
await table.UpdateAsync();
The same applies for IValueInsertable
var optional1 = Optional<int>.None();
var optional2 = Optional<int>.Of(123);
var table = Database.MyTable; // ITable<MyRecord>
if (optional1.HasValue) table = table.Value(x => x.Optional1, optional1.Value);
if (optional2.HasValue) table = table.Value(x => x.Optional2, optional2.Value);
await table.InsertAsync();

You can use AsUpdatable()
var updatable = Database.MyTable.AsUpdatable();
if (optional1.HasValue) updatable = updatable.Set(x => x.Optional1, optional1.Value);
if (optional2.HasValue) updatable = updatable.Set(x => x.Optional2, optional2.Value);
await updatable.UpdateAsync();

Related

Binding row value to odata model SAP UI5

I have a table with 2 columns - one where user can input a value to return and other column is a checkbox. If a user enters a value in the item row, I make the checkbox checked. If value is greater than 0 then only the checkbox is selected. My issue is with the below code, if I input a value on the 3 rd row, that checkbox is selected but alongside even the first row's checkbox is selected. I think the issue is in the stmt: tableModel.setProperty("/ItemSet/results/0/ReturnItemFlag", "X"); Because I am giving "0" the first row is also getting the value. How to I point to the correct result number.
Controller.js
qtyChange: function(oEvent) {
var a = oEvent.getSource();
var input = a.getValue()
var row = oEvent.getSource().getParent().getParent();
var index = row.getIndex();
var oTable = vc.getView().byId("takeStockHistoryDetailTable");
var selectedRowPath = oTable.getContextByIndex(index).getPath();
var tableModel = vc.getView().getModel(TAKE_STOCK_ORDER_DETAIL);
var selectedPart = tableModel.getObject(selectedRowPath);
var QtyOnHand = selectedPart.QtyOnHand;
var UnitP = selectedPart.UnitPrice;
var f = parseInt(input);
var g = parseInt(QtyOnHand);
var h = parseFloat(UnitP);
if (f > g) {
sap.m.MessageToast.show("Return quantity is more than available quantity");
a.setValue("");
} else if (f === 0 || input === "") {
selectedPart.ReturnItemFlag = 'Y';
tableModel.setProperty("/ItemSet/results/0/ReturnItemFlag", "Y");
} else {
selectedPart.ReturnItemFlag = 'X';
selectedPart.QtyToReturn = input;
var sub = input * h;
// debugger;
var sub1 = sub.toString();
selectedPart.Subtotal = sub1;
tableModel.setProperty("/ItemSet/results/0/ReturnItemFlag", "X");
tableModel.setProperty("/ItemSet/results/0/Subtotal", sub1);
}
},
This is possibly a very complicated way of working with table items.
Here is how you should work with bindingContexts.
on listItemPress Event of the table(list)
qtyChange: function(oEvent){
var oColumnListItem = oEvent.getSource().getParent();
var sPath = oColumnListItem.getBindingContextPath("yourModelName");
OR
var sPath = oColumnListItem.getBindingContext("yourModelName").getPath();
var sReturnItemFlagPath = sPath + "/ReturnItemFlag";
tableModel.setProperty(sReturnItemFlagPath,"newValue");
}
Let me know if this helps!

Combine/merge multiple maps into 1 map

How can I combine/merge 2 or more maps in dart into 1 map?
for example I have something like:
var firstMap = {"1":"2"};
var secondMap = {"1":"2"};
var thirdMap = {"1":"2"};
I want:
var finalMap = {"1":"2", "1":"2", "1":"2"};
you can use addAll method of Map object
var firstMap = {"1":"2"};
var secondMap = {"2":"3"};
var thirdMap = {};
thirdMap.addAll(firstMap);
thirdMap.addAll(secondMap);
print(thirdMap);
Or
var thirdMap = {}..addAll(firstMap)..addAll(secondMap);
Update
Since dart sdk 2.3
You can use spread operator ...
final firstMap = {"1":"2"};
final secondMap = {"2":"3"};
final thirdMap = {
...firstMap,
...secondMap,
};
alternate syntax using Map.addAll, Iterable.reduce and cascading operator, for combining a lot of maps:
var combinedMap = mapList.reduce( (map1, map2) => map1..addAll(map2) );
live dartpad example
https://dartpad.dartlang.org/9cd116d07d2f45a9b890b4b1186dcc5e
Another option is using CombinedMapView from package:collection:
new CombinedMapView([firstMap, secondMap])
It doesn't create a merged map, but creates a Map that is a view of both.
I came up with a "single line" solution for an Iterable of Maps:
var finalMap = Map.fromEntries(mapList.expand((map) => map.entries));
var firstMap = {"1":"5"};
var secondMap = {"1":"6"};
var thirdMap = {"2":"7"};
var finalMap = {...firstMap, ...secondMap, ...thirdMap};
// finalMap: {"1":"6", "2":"7"};
Notice that key "1" with value "5" will be overwritten with "6".

Proper return type in MVC WEB API after join table result set

This works in my web api:
public IEnumerable<Object> Get()
{
var passengerVehicle =
from vehMake in _Uow.GetRepository<VehicleMake>().Include<VehicleModel>()
join passVeh in _Uow.GetRepository<PassengerVehicle>().Include<Vehicle>()
.Include(y => y.Vehicle.VehicleModel)
.Include(y => y.Vehicle.VehicleSubModel)
.Include(y => y.Vehicle.VehicleImages)
.Include(y => y.Vehicle.VehicleLocation)
.Include(y => y.Vehicle.VehiclePrices)
on vehMake.Id equals passVeh.Vehicle.VehicleModel.FK_VehicleMakeId
select new
{
vehMake = vehMake.Name,
vehMod = passVeh.Vehicle.VehicleModel.Name,
vehSubModel = passVeh.Vehicle.VehicleSubModel.Name,
vehLocation = passVeh.Vehicle.VehicleLocation,
vehImages = passVeh.Vehicle.VehicleImages.ToList(),
vehOptions = passVeh.Vehicle.VehicleOptions.ToList(),
vehPostHistories = passVeh.Vehicle.VehiclePostHistories.ToList(),
vehStats = passVeh.Vehicle.VehicleStatistics.ToList(),
vehStatus = passVeh.Vehicle.VehicleStatus,
vehPrices = passVeh.Vehicle.VehiclePrices.ToList(),
};
return passengerVehicle;
}
I am not delighted that the return type is "object"
I thought perhaps a HttpResponse but apparently that is not correct either.
PassengerVehicle has an association with Vehicle.
Vehicle has an association with VehicleModel & VehicleSubModel.
VehicleModel has an association with VehicleMake.
Pretty much everything else are primitives or straight up collections.
Suggestions?
It would be better to use ViewModel, like:
select new VehicleSpecialViewModel
{
vehMake = vehMake.Name,
vehMod = passVeh.Vehicle.VehicleModel.Name,
vehSubModel = passVeh.Vehicle.VehicleSubModel.Name,
vehLocation = passVeh.Vehicle.VehicleLocation,
vehImages = passVeh.Vehicle.VehicleImages.ToList(),
vehOptions = passVeh.Vehicle.VehicleOptions.ToList(),
vehPostHistories = passVeh.Vehicle.VehiclePostHistories.ToList(),
vehStats = passVeh.Vehicle.VehicleStatistics.ToList(),
vehStatus = passVeh.Vehicle.VehicleStatus,
vehPrices = passVeh.Vehicle.VehiclePrices.ToList(),
};
and then your return type would be
IEnumerable<VehicleSpecialViewModel>

IBuildDetail.RequestedFor doesn't get populated after calling QueryBuilds()

I call IBuildServer.QueryBuilds(), and look at the returned IBuildDetails:
TFS2010: They are populated with user aliases.
TFS2012: They are null.
How can I get the user names in TFS2012?
var collection = new TfsTeamProjectCollection(new Uri(_txtCollection.Text));
var buildServer = collection.GetService<IBuildServer>();
var spec = buildServer.CreateBuildDetailSpec(Settings.Default.ProjectName);
spec.QueryOptions = QueryOptions.Definitions;
spec.QueryOrder = BuildQueryOrder.StartTimeDescending;
spec.InformationTypes = null;
return buildServer.QueryBuilds(spec).Builds;
Add batchedrequests:
spec.QueryOptions = QueryOptions.Definitions
| QueryOptions.BatchedRequests; // Tell TFS2012 to populate RequestedFor / RequestedBy

Is there an easier way than this to edit a table in my database?

I’m trying to update a row in a table. I found a way to make it work but I’m wondering if there is an easier or better way. Please help me with any ideas that you may have. Thanks
Here’s the code…
var courseRubics = from r in db.Rubrics where r.DepartmentID == 2 select r;
var selectedRubrics = courseRubics.Select(r => r.RubricID);
List<int> rubricsList = selectedRubrics.ToList();
foreach (var rub in courseRubics.ToList())
{
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
Rubric updateRubic1 = (Rubric)aRubic1;
int rubric1 = Convert.ToInt32(totlrubric[0]);
updateRubic1.Weight = rubric1;
Rubric aRubic2 = db.Rubrics.Find(2);
Rubric updateRubic2 = (Rubric)aRubic2;
int rubric2 = Convert.ToInt32(totlrubric[1]);
updateRubic2.Weight = rubric2;
Rubric aRubic3 = db.Rubrics.Find(4);
Rubric updateRubic3 = (Rubric)aRubic3;
int rubric3 = Convert.ToInt32(totlrubric[2]);
updateRubic3.Weight = rubric3;
}
db.SaveChanges();
}
Couple of ideas / observations
Your initial set of selects could be narrowed down to (sorry for the alternate syntax)
var list = db.Rubrics.Where(r => r.DepartmentId == 2).Select(x => x.RubricId).ToList()
You iterate over foreach (var rub in courseRubics.ToList()) but don't seem to be doing anything with rub
The contents of the if... could be tightened up a little. I don't see any reason why you need to do the casting or why you can't just inline the creation of rubricX,
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
aRubic1.Weight = Convert.ToInt32(totlrubric[0]);
Rubric aRubic2 = db.Rubrics.Find(2);
aRubic2.Weight = Convert.ToInt32(totlrubric[1]);
Rubric aRubic3 = db.Rubrics.Find(4);
aRubic3.Weight = Convert.ToInt32(totlrubric[2]);
}

Resources