I was wondering if there is way of converting lists with complex numbers into list with real numbers.
[3.084580633850594 + 0.0i, 0.03930937672422563 + 0.0i, 25958.26505623091 + 0.0i, 28566.521479745476 + 0.0i]
The way in which i did it was by using.
for (var item in list){
double real = item.real;
}
https://pub.dartlang.org/documentation/my_complex/latest/my_complex/Complex/real.html
If you have an existing list of complex numbers using some appropriate Complex number class, using List.map would be neater:
var realParts = complexNumbers.map((z) => z.real);
Note that that will give you an Iterable; if you want a List, you'll need to call Iterable.toList:
var realParts = complexNumbers.map((z) => z.real).toList();
Related
I have a regex expression that I'm trying to create dynamically.
RegExp reg4 = RegExp(r'(two+\s\w+\s+one)');
My intention is to replace the value at two to be set dynamically
I've tried this
var two = "two";
RegExp reg4 = RegExp(r'(' + two + '+\s\w+\s+one)');
But it doesn't work.
You forgot the r on the second part of the string:
var two = "two";
RegExp reg4 = RegExp(r'(' + two + r'+\s\w+\s+one)');
// ^ <- that one!
Without that r, the \ss and \w in the second string are interpreted as string escapes, and disappear before they get to the RegExp parser.
I'll also point out that the result of (two+\s\w+\s+one) has a + applying to only the o of two.
You might want to create ((?:two)+\s\w+\s+one) instead, so you repeat the entire "two" string.
Another thing to consider is whether you want to match the two variable's string verbatim, or if it can contain a RegExp too.
If you want it verbatim, so that a value of var two = "[NOTE]"; won't match a single character, the string should be escaped:
RegExp reg4 = RegExp(r'((?:' + RegExp.escape(two) + r')+\s\w\s+one)');
I have a situation where I have a list that can be at most 4 elements.
However, if I have only 1-3 elements to put in that list, how can I fill the remainder with null values?
For example, a List<int?> of length 4 with 2 given elements, should result in:
[1,3] -> [1,3,null,null]
Here's what I'm doing, but maybe there is a better way
List.generate(4, (index) {
try {
final id = given.elementAt(index);
return id;
} catch (error) {
return null;
}
});
The simplest version would probably be:
[for (var i = 0; i < 4; i++) i < given.length ? given[i] : null]
You can use List.generate, but in this case, the function is simple enough that a list literal can do the same thing more efficiently.
(Or, as #jamesdlin says, if you want a fixed-length list, use List.generate).
A more indirect variant could be:
List<GivenType?>.filled(4, null)..setAll(0, given)
where you create a list of four nulls first, then write the given list into it. It's probably just more complicated and less efficient.
below is my Action Method.It return Last item of list only. but I want list of Items in AList.
public ActionResult Ataxi(){
List<sub_employee> AList = new List<sub_employee>();
var alist = IM.getAvailableList().ToList();
foreach(var item in alist)
{
AList = db.sub_employee.Where(s => s.SE_ID == item).ToList();
}
return View(AList);
}
how do I get All elements in Alist. Can Somebody help me to solve this problem. thank you
I think you want something like this:
public ActionResult Ataxi(){
var list1 = IM.getAvailableList().ToList();
var list2 = db.sub_employee
.Where(x => list1.Contains(x.Id))
.ToList();
return View(list2);
}
In your example you keep overwriting the value of the list. You also check Where for each item of your original list against db.sub_employee, which is hard to read and not very efficient. You really only need to use Where once to filter the value whose key is not already in the list. Note that using Contains inside Where is horribly inefficient, but its simple to write and doesn't require creating new LINQ operators.
Also, on a style note, I would avoid starting local variable names with capital letters (Alist), and especially avoid local variables that only differ by capitalization (Alist vs alist). Conversely, it is standard to name types and properties starting with capital letters (sub_employee).
I clearly don't know what I'm doing. This MVC stuff is really blowing my mind in trying to keep with the pattern. I've been following the MVC tutorials as well as mega-googling and this is the corner I've painted myself into.
I have multiple similar pieces of data I'm trying to get to a view. I'm able to get my code to work, but to me it just looks like it's going to be highly inefficient as we start pulling large recordsets from the db due to multiple calls to the db. So, I have a OrderSummary class, inside the class is this:
public IEnumerable<Order> GetOrders()
{
var orders = (from s in db.Orders
where s.UserId == uId
select s);
return orders.ToList();
}
Then this:
public decimal GetGrossProfitTotal()
{
var orders = (from s in db.Orders
where s.UserId == uId
select s);
decimal? grossprofittotal = orders.Sum(s => s.Profit);
return grossprofittotal ?? decimal.Zero;
}
So, if we take that last chunk of code and copy it for totalcommission and netprofittotal that's basically how I have things layed out. I would guess four calls to the db?
Then in the controller:
var ordersummary = new OrdersSummary();
var viewModel = new OrderSummary
{
Orders = ordersummary.GetOrders(),
GrossProfitTotal = ordersummary.GetGrossProfitTotal(),
CommissionTotal = ordersummary.GetCommissionTotal(),
NetProfitTotal = ordersummary.GetNetProfitTotal(),
};
return View(viewModel);
This gets me all the data I need in the view so I can work with it. To me, it just seems unnecessarily redundant and I'm guessing inefficient? If you throw in that I'm also doing sort and search parms, it's a lot of duplicate linq code as well. It seems like I should be able to do something to consolidate the data like this:
var orders = (from s in db.Orders
where s.UserId == uId
select s).ToList();
decimal grossprofittotal = orders.Sum(s => s.Profit);
decimal commissiontotal = orders.Sum(s => s.Commission);
decimal netprofittotal = orders.Sum(s => s.Profit + s.Commission);
and then wrap those four pieces of data (orders list, and three decimal values) up nicely in an array (or whatever) and send them to the controller/view. In the view I need to be able to loop through the orders list. Am I way off here? Or, what is standard procedure here with MVC? Thanks.
Yes, fetching the same data four times is indeed inefficient, and completely unneccesary. You can very well fetch it only once and then do the other operations on the data that you have.
You can keep the GetOrders method as it is if you like, but that's all the data that you need to fetch. If you fetch the data in the controller or in the model constructor is mostly a matter of taste. Personally I tend to put more logic in the model than the controller.
As long as you use ToList to make sure that you actually fetch the data (or any other method that realises the result as a collection), you can calculate the sums from what you have in memory. (Without it, you would still be doing four queries to the database.)
Instead of summing up the profit and commision from all items to get the net profit total, you can just calculate it from the other sums:
decimal netprofittotal = grossprofittotal + netprofittotal;
LinqToEntities tranlates all query into SQL. If you don't want to make more than one transaction, you can fetch the result into a variable by .ToList(),querying this object make the calculation by linqToObject in the memory.
Backward: It fetchs all orders from database first.
var ordersInMemory = orders.ToList();
decimal grossprofittotal = ordersInMemory.Sum(s => s.Profit);
decimal commissiontotal = ordersInMemory.Sum(s => s.Commission);
decimal netprofittotal = grossprofittotal + commissiontotal ;
All I want to do is
Get the name-value pairs that were supplied to Request.QueryString
Populate a javascript object (aka hash) with keys from the names and values from the values
Halt the page if one of the expected hash values is the empty string
The Request.QueryString object is reminding me why I hated classic asp even before it was an abandoned technology. :/
The Request.QueryString collection has an awkward interface, particularly when it comes to iterating or cases where there are multiple params with the same name. I suggest grabbing the whole querystring using Request.QueryString.Item() or Request.ServerVariables('QUERY_STRING') and parse it using unescape/decodeURIComponent. It's a bit of effort, but gives you more control and consistency.
A simple example that lowercases keys:
var params = parseQueryString(Request.QueryString.Item());
function parseQueryString(qs) {
var parsed = {}, pairs = qs.split('&');
for (var i = 0; i < pairs.length; i ++) {
var pair = pairs[i], pos = pair.indexOf('=');
if (pos < 0) pos = pair.length;
parsed[unescape(pair.slice(0, pos)).toLowerCase()] = unescape(pair.slice(pos + 1));
}
return parsed;
}
Querystring contents are treated as string by default i believe..
But if you have to, you can always do String( request.querystring("foo") );
Are the keys known before-hand ? or you want to iterate through the pairs and retrieve both key and value ?