|
As stated by other members, no will solve YOUR problem, you will have to do that yourself. We do however lend a helping hand in nudging you in the right direction.
I suggest you google "C# Stream class" and read up on how it actually works.
|
|
|
|
|
Good evening I have a problem with my XAML designer. I find this error:
System.ArgumentNullException
The value can not be null.
Parameter name: remoteValues
to Microsoft.Expression.DesignHost.Isolation.Remoting.LocalNotifyingEnumerable`2..ctor (IRemoteNotifyingEnumerable`1 remoteValues, Func`2 converter)
to Microsoft.Expression.DesignHost.Isolation.Remoting.LocalDesignerView.Microsoft.Expression.DesignHost.IDesignerView.get_RelatedDocumentPaths ()
to Microsoft.Expression.DesignHost.IsolatedDesignerService.IsolatedDesignerView.CreateDesignerViewInfo (CancellationToken cancelToken)
|
|
|
|
|
var usersWithRoles = (from user in context.Users
select new
{
UserId = user.Id,
Username = user.UserName,
Email = user.Email,
RoleNames = (from userRole in user.Roles
join role in context.Roles on userRole.RoleId
equals role.Id
select role.Name).ToList()
}).ToList().Select(p => new Users_in_Role_ViewModel()
{
UserId = p.UserId,
Username = p.Username,
Email = p.Email,
Role = string.Join(",", p.RoleNames)
});
guide me how could i compose the above query using lambda if possible. thanks
|
|
|
|
|
You already have a Lambda in there:
Select(p => new Users_in_Role_ViewModel() So what help do you need?
And why are you converting things to List<T> so often? All that does is "collapse" the iteration, which wastes time and memory space...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
you said :- why are you converting things to List<t> so often? All that does is "collapse" the iteration, which wastes time and memory space...
so tell me which area need to change in query?
|
|
|
|
|
Sir Morning,
you said "And why are you converting things to List<t> so often? All that does is "collapse" the iteration, which wastes time and memory space..."
which still not clear that what you try to say?
are pointing why i use tolist()...what problem may occur for this. please make me aware in details so i may avoid this kind of mistake in future. thanks
|
|
|
|
|
Linq queries and Linq methods aren't executed immediately, they use something called "Deferred execution" which (basically) means that nothing is done until you actually need the final results.
There is a basic example of this here: Deferred Execution Example (C#) | Microsoft Docs[^]
Deferred execution means better efficiency (sometimes) because you can "combine operations" instead of having to loop each time to produce the intermediate result which you then pass to the next section of the evaluation. Remember: Linq isn't "no loops" it's about "hiding loops" where you can't see them and evaluating them only when it has to.
But ... when you say "ToList" you are explicitly requesting an object be created holding the whole results - so the whole operation has to be performed immediately to generate the collection you asked for. So when you then continue to process it, you need to do another loop to process the next operation.
Every time you call ToList, you make Linq work harder, and that takes both more time, and uses more memory. Only ever "Collapse the operation" when you actually need the results!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Something like this should work:
var usersWithRoles = context.Users
.Select(user => new
{
UserId = user.Id,
Username = user.UserName,
Email = user.Email,
RoleNames = user.Roles.Join(context.Roles, userRole => userRole.RoleId, role => role.Id, (userRole, role) => role.Name)
})
.AsEnumerable()
.Select(p => new Users_in_Role_ViewModel
{
UserId = p.UserId,
Username = p.Username,
Email = p.Email,
Role = string.Join(",", p.RoleNames)
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thank you sir
|
|
|
|
|
sir,
you use .AsEnumerable() where as i used .ToList()....what would be the difference in term of performance of using .AsEnumerable(). if possible please explain this one with a easy example that what will happen internally to use .ToList() which causes performance issue.
tell me if i write this way then what will be the performance
var usersWithRoles = (from user in context.Users
select new
{
UserId = user.Id,
Username = user.UserName,
Email = user.Email,
RoleNames = (from userRole in user.Roles
join role in context.Roles on userRole.RoleId
equals role.Id
select role.Name)
}).ToList().Select(p => new Users_in_Role_ViewModel()
{
UserId = p.UserId,
Username = p.Username,
Email = p.Email,
Role = string.Join(",", p.RoleNames)
});
just inner tolist function......does it increase performance ?
you code as follows
var usersWithRoles = context.Users
.Select(user => new
{
UserId = user.Id,
Username = user.UserName,
Email = user.Email,
RoleNames = user.Roles.Join(context.Roles, userRole => userRole.RoleId, role => role.Id, (userRole, role) => role.Name)
})
.AsEnumerable()
.Select(p => new Users_in_Role_ViewModel
{
UserId = p.UserId,
Username = p.Username,
Email = p.Email,
Role = string.Join(",", p.RoleNames)
});
this is your join which not clear to me.
RoleNames = user.Roles.Join(context.Roles, userRole => userRole.RoleId, role => role.Id, (userRole, role) => role.Name)
the join is happening between which tables ?
if re-write this way then what will be performance
List<Users_inRole_ViewModel > usersWithRoles= db.Users.Include(a => a.Roles).Select(p =>
new Users_inRole_ViewModel {
UserId = p.Id,
Username = p.UserName,
Email = p.Email,
Role = string.Join(",",
db.Roles.Where(c=> p.Roles.Any(d => d.RoleId == c.Id )).Select(u=>u.Name).ToList() )
}
);
please help me to understand few points which i have mention here. thanks
modified 24-Jan-18 6:55am.
|
|
|
|
|
please tell me how i can plot contour plot from visual studio data in c#
|
|
|
|
|
Member 13624074 wrote: visual studio data Don't know what that is supposed to mean. However you probably have two choices: write your own plotting code, or use Google to find a tool that will do it for you.
|
|
|
|
|
Hey all,
So I'm coding on some random dungeon-gen, and up until now it's being going great.
I am getting the error Not all code paths return a value I looked into the error, tweaked my code a little and I'm still not making any progress. Let me post my code.
Quote: using UnityEngine;
using System.Collections.Generic;
public class DTileMap
{
protected class DRoom
{
public int left;
public int top;
public int width;
public int height;
public int right {
get {return left + width - 1;} }
public int bottom {
get { return top + height - 1; }
}
public bool CollidesWith(DRoom other) {
if( left > other.right-1 )
return false;
if( top > other.bottom-1 )
return false;
if( right < other.left+1 )
return false;
if( bottom < other.top+1 )
return false;
return true;
}
}
int size_x;
int size_y;
int[,] map_data;
List<droom> rooms;
public DTileMap (int size_x, int size_y)
{
this.size_x = size_x;
this.size_y = size_y;
rooms = new List<droom> ();
map_data = new int[size_x, size_y];
for (int i=0; i < 30; i++) {
int rsx = Random.Range (4,8);
int rsy = Random.Range (4,8);
DRoom r = new DRoom();
r.left = Random.Range(0, size_x - rsx);
r.top = Random.Range(0, size_y-rsy);
r.width = rsx;
r.height = rsy;
if(!RoomCollides(r)){
rooms.Add (r);
MakeRoom(r);
}
}
}
bool RoomCollides(DRoom r)
{
foreach (DRoom r2 in rooms)
{
if(r.CollidesWith(r2))
{
return true;
}
}
}
public int GetTileAt(int x, int y){
return map_data [x, y];
}
void MakeRoom(DRoom r)
{
for (int x=0; x < r.width; x++)
{
for (int y=0; y < r.height; y++)
{
if(x==0 || x == r.width -1 || y==0 || y==r.height -1)
{
map_data[r.left+x, r.top+y] = 3;
}
else
{
map_data [r.left + x, r.top + y] = 1;
}
}
}
}
}
// Id's
// 0 = Void
// 1 = Floor
// 2 = Random
// 3 = Wall
Now, the error code is pointing towards line 69, but I check all of my value on lines 21 - 34.
If you need any extra information, let me know. I've tried to figure this out, but I'm stumped...
Thanks in advance!
|
|
|
|
|
Use the line number to identify the function where the error occured and check the code of that function.
I don't want to count lines and your posted code might be out of sync, but the RoomCollides function does not return a value on all pathes:
bool RoomCollides(DRoom r)
{
foreach (DRoom r2 in rooms)
{
if(r.CollidesWith(r2))
{
return true;
}
}
}
|
|
|
|
|
Jochen has probably sorted that one fro you, but VS does make it really easy to find where an error is:
1) In the Errors pane, you can double click on an error and VS will take you directly to the offending line.
2) If you press CTRL+G, VS will popup a "go to line number" dialog allowing you to specify exactly which line in the file you want to look at.
When you get a compiler error, they are normally in reasonable english and pretty explanatory: "not all paths return a value" is pretty clear and self explanatory: not all possible routes through the current function reach either a return or a throw statement. You should be able to work out from that what is missing. Most other errors are equally understandable - just look at the line above if it isn't obvious, because sometimes missing out a semicolon, or a comma, or an open bracket, etc. can mean that the problem isn't detected until the following line.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have two issues with speech recognition in C#.
I think I have installed all necessary packages for speech recognition, what I know of:
* Microsoft Server Speech Platform runtime (x64)
* Microsoft Speech Platform SDK (x64) v11.0
* Microsoft Server Speeech Recognition Language - TELE (en - GB)
* Microsoft Server Speeech Recognition Language - TELE (en - US)
* Microsoft Server Speeech Recognition Language - TELE (sv - SE)
I'm from Sweden so my Win7x64 Home Premium is set to Swedish - Sweden. So I think my culture is sv - SE. (Keep that in mind if culture may cause my problems).
I copied the code from this page into a console application, added a reference to system.Speech.Recognition and it compiled.
Problem 1
When I ran it it crashed on this line:
foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers()) A NullReferenceException was thrown.
So I'm stuck there.
Problem 2
If I comment away the foreach loop for the RegognizerInfo and change the using statement to:
using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine()) It throws an UnauthorizedAccessException on this line:
recognizer.LoadGrammar(new DictationGrammar()); I ran VS2015 as administrator and I got the real exception: PlatformWasNotSupported. The exception says: "No interpreter installed" or "No recognizer installed". (I'm translating Swedish error messages to English in the best way I can).
So I'm stuck there as well.
Any suggestions what to no next?
|
|
|
|
|
Hi community,
so I am trying to insert the data from one database to another. My problem is I cannot insert the data with my data adapter. So following Code I am having right now:
SqlDataAdapter dadapter = new SqlDataAdapter() { AcceptChangesDuringFill = false };
dadapter.SelectCommand = new SqlCommand("SELECT X AS A, Y AS B FROM Tab1", cn1);
DataTable dt = new DataTable();
dadapter.Fill(dt);
OracleDataAdapter da = new OracleDataAdapter("SELECT A, B from Tab2", connOracle)
{
AcceptChangesDuringFill = false
};
OracleCommandBuilder cb = new OracleCommandBuilder(da);
da.Update(dt);
Im getting the error, that I need to to have an update statement for an insert.
But even when I try to add
da.InsertCommand = new OracleCommand("SELECT A, B from Tab2", connOracle);
it doesnt show any error but I it doesnt insert into my oracle db. Anyone knows what to do?
Thanks!
|
|
|
|
|
You are using a SELECT command, which reads from the database: you need either INSERT or UPDATE .
|
|
|
|
|
Member 13466176 wrote:
da.InsertCommand = new OracleCommand("SELECT A, B from Tab2", connOracle);
Erm, don't you think the insert command should perhaps insert something, rather than select something?
Something like this should work:
da.InsertCommand = new OracleCommand("INSERT INTO Tab2 (A, B) VALUES (:A, :B)", connOracle); (Assuming I've got the Oracle parameter syntax right...)
NB: You'll need to change all of the rows in your source table to the "Added" state so that the DataAdapter knows what to do with them.
...
dadapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
row.SetAdded();
}
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard,
doesnt the CommandBuilder automatically convert the SELECT statement into an Insert statement when I call da.Update(dt)?
Anyways, could you tell me how to bind the columns of the dataset dt to a parameter like you showed?
Thank you!
|
|
|
|
|
Member 13466176 wrote: doesnt the CommandBuilder automatically convert the SELECT statement into an Insert statement when I call da.Update(dt)?
IIRC, it should do. But the error you're getting suggests that's not working for the OracleCommandBuilder for some reason.
Member 13466176 wrote: Anyways, could you tell me how to bind the columns of the dataset dt to a parameter like you showed?
It's a while since I used a DataAdapter to modify data, but something like this should work:
da.InsertCommand = new OracleCommand("INSERT INTO Tab2 (A, B) VALUES (:A, :B)", connOracle);
var p = da.InsertCommand.CreateParameter();
p.ParameterName = "A";
p.SourceColumn = "A";
p.SourceVersion = DataRowVersion.Current;
da.InsertCommand.Parameters.Add(p);
p = da.InsertCommand.CreateParameter();
p.ParameterName = "B";
p.SourceColumn = "B";
p.SourceVersion = DataRowVersion.Current;
da.InsertCommand.Parameters.Add(p);
da.Update(dt);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
YOU ARE THE BEST!!!!
It worked!
I thank you alot, Richard
|
|
|
|
|
Hi,
Im trying to send "1C0" thru serial port to a device, and the device is responding and are sending back "52" in TeraTerm.
But with this code, the device is responding to the sent request, but I get this error and the answer of "52" is not shown in the program.
What is wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ArduinoSerialTemp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.BaudRate = 9600;
serialPort1.PortName = "COM44";
serialPort1.Open();
serialPort1.Write("*1C0");
serialPort1.Write("\r");
serialPort1.DataReceived += serialPort1_DataReceived;
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string line = serialPort1.ReadLine();
this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
}
private delegate void LineReceivedEvent(string line);
private void LineReceived(string line)
{
textBox1.Text = line;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
{
serialPort1.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
|
|
|
|
|
Without running your code, we can't really tell - but at a guess it's the ReadLine call that is timing out that causes the problem. You send just a '\r' as the "end of line" code, so it's possible that the device response is not terminated with what the system regards as a newline. Me? I'd read the data that is available, and store it in a buffer instead of trying to read a line. That way, I get to process the data any way I need, and can look at exactly what is being returned.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
In which line of your code does the error occur?
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|