 |

|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|

|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|

|
Say I have these in-memory objects:
Team object. It has 2 properties:
Nickname (string)
City (another object)
City object. It has 1 property:
Population (int)
Using LINQ, how do I select the Nickname and Population into the same result? I tend to isolate the City object, then join to it using Population. Isn't there a better way?
Marty
|
|
|
|

|
I'm assuming you have a list of Teams, but given:
public class Team
{
public string Nickname { get; set; }
public City City { get; set; }
}
public class City
{
public int Population { get; set; }
}
I suspect you're looking for something like ...
IEnumerable<Team> teams = getMeSomeTeamsDagnamit();
var result =
from t in teams
select new { t.Nickname, t.City.Population };
I'd suggest you read up on Anonymous Types [^] if you're not familiar.
|
|
|
|

|
I realize now that my original sample code was too simplistic. In reality, the "Team" object was an (incorrect) projection from a "groupby", where the "City" wasn't available. We figured it out. Thanks for the info!
|
|
|
|

|
Hi !
I have some codes below here:
DataTable objTable = new DataTable();
objTable.Columns.Add("ID");
objTable.Columns.Add("Name");
objTable.Columns.Add("Root");
DataRow objrow1 = objTable.NewRow();
objrow1["ID"] = "1";
objrow1["Name"] = "1";
objrow1["Root"] = "";
objTable.Rows.Add(objrow1);
objrow1 = objTable.NewRow();
objrow1["ID"] = "11";
objrow1["Name"] = "11";
objrow1["Root"] = "1";
objTable.Rows.Add(objrow1);
objrow1 = objTable.NewRow();
objrow1["ID"] = "12";
objrow1["Name"] = "12";
objrow1["Root"] = "1";
objTable.Rows.Add(objrow1);
objrow1 = objTable.NewRow();
objrow1["ID"] = "121";
objrow1["Name"] = "121";
objrow1["Root"] = "12";
objTable.Rows.Add(objrow1);
objrow1 = objTable.NewRow();
objrow1["ID"] = "2";
objrow1["Name"] = "2";
objrow1["Root"] = "";
objTable.Rows.Add(objrow1);
objrow1 = objTable.NewRow();
objrow1["ID"] = "21";
objrow1["Name"] = "21";
objrow1["Root"] = "2";
objTable.Rows.Add(objrow1);
And now, how can I list item and subitems in root is 1?Please help me !!!!
MCP.NET, MCAD.NET, MCSD.NET
|
|
|
|

|
you will need to make sure that there is a reference to System.Data.DataSetExtensions in your project.
var Results = from data in objTable.AsEnumerable()
where data.Field<int>("Root") == 1
select data
further reading
Linq query on a datatable[^]
Queries in LINQ to DataSet[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|

|
Thanks !
It return two items: 11 and 12. But sub items of 12? How can I get its?
MCP.NET, MCAD.NET, MCSD.NET
|
|
|
|

|
Hi All, I've spent way to long on this - I'm trying to get distinct values from a dataTable: begDate = 2007-11-18 / endDate = 2007-12-31 Why does this work: Returns 4 rows Dim shipTo = From row In osDS.Tables(0).AsEnumerable _ Order By row(7) _ Where row(0).ToString = cust _ And row(1).ToString > begDate _ Select row(7) Distinct This doesn't work: Results Empty / Enumeration yield no results.... Dim shipTo = From row In osDS.Tables(0).AsEnumerable _ Order By row(7) _ Where row(0).ToString = cust _ And row(1).ToString < endDate _ Select row(7) Distinct But what I really want is: (but it doesn't work either) Dim shipTo = From row In osDS.Tables(0).AsEnumerable _ Order By row(7) _ Where row(0).ToString = cust _ And row(1).ToString > begDate _ And row(1).ToString < endDate _ Select row(7) Distinct Thanks in advance . . . . MB
|
|
|
|

|
Are begDate and endDate actually strings with the values "2007-11-18", "2007-12-31" respectively?
And what is the type of data in row(1)? Is it also a string, or is it a date?
If it is a date in that column, then it seems possible that taking the ToString() from date values is not using the same format as begDate and endDate are representing the date.
I'd suggest doing the comparison as date (DateTime) values to avoid any issues with cultural-specific date formatting.
|
|
|
|

|
Hi,
I have a comma separated text file and I want to get all SerialNumber for a specific device. Example: I want to get the SerialNumber for all Device:A1 => 345 and 347
ID:432,Name:Jones,SerialNumber:345,Device:A1
ID:432,Name:Mark,SerialNumber:347,Device:A1
ID:432,Name:Ann,SerialNumber:346,Device:A2
I have now solved this problem with a combination of linq, foreach and regex. How can I do this in one linq expression?
Best regards
Olof
|
|
|
|

|
Try to look at this LINQ to CSV library. I briefly explored it and seems it allows to get IEnumerable<T> from *.csv file and then use LINQ against it.
|
|
|
|

|
select a.LoanNum, PromiseAmt, isnull(SUM(PdAmt), 0) as PostedAmt
from LoanPromises a with(nolock)
left outer join LoanPayment lp with(nolock) on a.LoanNum = lp.LoanNum and a.PromiseDt < lp.PmtPostDt
group by a.LoanNum, PromiseAmt
having isnull(Sum(PdAmt),0) < PromiseAmt
Here is my linq query
var query = from pmts in oLoan.LoanPayments
join promise in oLoan.LoanPromiseToPays
on new { p1 = pmts.LoanNum, p2 = pmts.PmtPostDt} equals new {LoanNum = promise.LoanNum, PmtPostDt = promise.PromiseDt } into outer
from defaulted in outer.DefaultIfEmpty()
group defaulted by promise.LoanNum && promise.PromiseAmt into grouped
select new
{
LoanNum = grouped.Key,
Sum = grouped.Sum(t => t.PdAmt < t.PromiseAmt),
PmtPostDt = promise.PromiseDt ?? default(DateTime)
}
|
|
|
|

|
Try taking out the p1 = and p2 = from the LHS of your join.
At the moment, you have:
on new { p1 = ..., p2 = ...} equals new { LoanNum = ..., PmtPostDt = ... }
The compiler has no way to map one key to the other, since the property names are different.
Also, your LINQ join won't match your SQL join. The SQL has promise.PromiseDate < payment.PmtPostDt, but your LINQ query has promise.PromiseDate = payment.PmtPostDt instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|

|
Please give me tips to study LINQ easily
|
|
|
|

|
Install LINQPad[^]. Try out the samples in it. Buy the book it's associated with and read that.
|
|
|
|
|
|
|
 |
Message Automatically Removed
|
|
|
|

|
In a C# 2010 application that I am working on, I want to use linq to sql to update 2 tables in a sql server 2008 database.
I want to do the following:
create new Table1 object ;
InsertOnSubmit(tbl1)
**Table 1 will contain the primary key.
create new Table2 object;
table2 will contain a foreign key column that refers to primary key in
table1 object.
InsertOnSubmit(tbl2)
SubmitChanges()
Before and/or right after the submitchanges() event occurs, I would like to know what the value is for the primary key in table1 and the foreign key value is in table2. I would like to know what the table key value is so I can display this information on reports that will be generated right after the record(s) have been inserted into the database. Thus can you tell me how to determine what the primary key value is for table1 that also refers to the foreign key value in table 2?
|
|
|
|

|
Hi,
using (TransactionScope ts = new TransactionScope())
{
InsertOnSubmit(tbl1);
SubmitChanges();
int val1= tbl1.PrimaryKey;
InsertOnSubmit(tbl2);
SubmitChanges();
int val2= tbl1.foreignKey;
if (strError == "")
{
SubmitChanges();
ts.Complete(); }
}
|
|
|
|

|
hi
I have a stored procedure in SQL Server 2005 for inserting in a table ; when I'm executing this procedure it works but also return a minus value?!why? what a minus returned value means here?
Thank you...
my code in C#:
string connSt = "Data Source=localhost;Initial Catalog=Status;Integrated Security=true;User ID=sa;Password=45637;Timeout=20000";
SqlConnection conn1 = new SqlConnection(connSt);
conn1.Open();
SqlCommand command = new SqlCommand("insertIninterface", conn1);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@interfaceName", SqlDbType.NVarChar).Value = textBox1.Text;
command.Parameters.Add("@interfaceIP", SqlDbType.NVarChar).Value = textBox2.Text;
command.Parameters.Add("@interfaceType", SqlDbType.Int).Value = textBox3.Text;
command.Parameters.Add("@maxValAllowed", SqlDbType.Int).Value = textBox4.Text;
command.Parameters.Add("@status", SqlDbType.Int).Value = textBox5.Text;
int rows = command.ExecuteNonQuery();
conn1.Close();
if (rows == 1)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Failed");
}
my stored procedure in SQL Server 2005:
@interfaceName nvarchar(50) ,
@interfaceIP nvarchar(50) ,
@interfaceType int ,
@maxValAllowed int ,
@status int
AS
BEGIN
SET NOCOUNT ON;
insert into interface(interfaceName,interfaceIP,interfaceType,maxValAllowed,status) values(@interfaceName,@interfaceIP,@interfaceType,@maxValAllowed,@status)
END
|
|
|
|

|
What does this have to do with LINQ?
When SET NOCOUNT is ON, the count is not returned.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|

|
The SP isn't returning a value. The 'return value' could be coming from anywhere, even being just what was lying around in some piece of memory from the previous query. If you want to check that the insert statement worked, don't SET NOCOUNT and return the result of it from the SP.
|
|
|
|
 |