2D Array get number of not null rows [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm stuck in a problem.
I have a class :
public class CustomerViewModel
{
public string Name;
public string[][] values;
public bool[] flag;
}
I wish to get count of rows in values[][] where row !=null

Assuming you already have:
var vm = new CustomerViewModel();
And have populated the values array, then..
For a count of non-null rows try:
var count = vm.values.Count(i => i != null);
Or for all rows where values[row][0] is not null:
var count = vm.values.Count(i => i != null && i.Length > 0 && i[0] != null);

Related

Convert string to generic type c# (Convert string to T) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a method that needs to convert a string to the generic type:
T GetValue<T>(string name)
{
string item = getstuff(name);
return item converted to T // ????????
}
T could be int or date.
you can use Convert.ChangeType
T GetValue<T>(string name)
{
string item = getstuff(name);
return (T)Convert.ChangeType(item, typeof(T));
}
if you need to limit input types only for int and DateTime, add condition like below
if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
// do something with other types
}

Condensing this logic with linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I convert the following logic to LINQ?
Flaggedlist is a type of List<string>. request.flagged is a numeric value in a request POCO.
if (request.Flagged == 1)
{
if (!patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
}
else if (request.Flagged == 0)
{
string usrid = flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>();
flaggedList.Remove(usrid);
}
It can be boiled down to two simple IF statements and the 2nd part shortened further still:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
flaggedList.Add(request.UserId);
if (request.Flagged == 0)
flaggedList.Remove(flaggedList.FirstOrDefault(a => a == request.UserId));
This is pretty condensed and clear, not sure why you'd want to make it more condensed.
If you really want to make it shorter, maybe you can do this:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
else if (request.Flagged == 0)
{
flaggedList.Remove(flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>());
}

Fastest algorithm to compare two collections or list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need the fastest algorithm in .NET C# to compare two large collections (200000 records in each collection). I need to validate each row of collection 1 with each row of collection 2 and return the row of collection 1 which has duplicate records in collection 2.
Please suggest a linq query or lookup table which ever is faster..The records are like A2368FG,AD5686,B678SD,C68AGFD,...
private bool CheckValidCode(string stdCode, List<COde> CodeMap, out int count)
{
bool bRetVal = true;
count = 1;
try
{
List<COde> tempCodeMap = new List<COde>();
for (int i = 0; i < CodeMap.Count; i++)
{
if (CodeMap[i].StandardCode == (stdCode))
{
tempCodeMap .Add(customerCodeMappings[i]);
if (CodeMap[i + 1].StandardCode == (stdCode))
{
tempCodeMap .Add(CodeMap[i + 1]);
}
break;
}
}
return tempCodeMap ;
}
}
Are they simple string objects in each? If so, you can use something like
Collection1.Intersect(collection2)
Which will return all record that exist in both collections.
Is that what you wanted? It is not clear from your question if you want to find records that exist in collection1 and multiple times in collection2. If that is what you want, you will need to dig deeper.
Methods like Intersect() etc. should help.
Don't use collections, use Set<T> classes (or convert your collections to sets).
Then you can call methods like Intersect(), it is just faster (but you trade of memory for speed)

Get the parameter count in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class abc
{
public object test(params object[] par)
{
//I need Count of the parameter here which means to check par contains 1 or 1,2
}
}
I access the class like,
abc obj =new abc();
obj.test(1);
(or)
obj.test(1,2);
my question is, there is possible to send 1 or 1,2 .I need the count of the How many parameters are in the Object in test class?How to do this?
Use Array.Length property.
public object test(params object[] par)
{
var count = par == null ? 0 : par.Length;
}
You can use Length property of object array.
public object test(params object[] par)
{
int length = par == null ? 0 : par.Length;
}
You can use Length property
Try This:
int len=par.Length;

Take the info from json file c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have Json file in string (for example):
#{
"Url": "http://site.com/?q=windows8"
}
How can i take the information after ?q= on c# (windows 8). Sorry for my English.
You can use the querystring.
in Codebehind file
public String q
{
get
{
if (Request.QueryString["q"] == null)
return String.Empty;
return Convert.ToString(Request.QueryString["q"]);
}
}
then use the line below to get the value
var index = ('<%=q%>');
You can do simply this :
string s = "myURL/?q=windows8";
// Loop through all instances of ?q=
int i = 0;
while ((i = s.IndexOf("?q=", i)) != -1)
{
// Print out the substring. Here : windows8
Console.WriteLine(s.Substring(i));
// Increment the index.
i++;
}

Categories