Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
if (resourceinfo != null && resourceinfo.Products != null)
{
foreach (var product in resourceinfo.Products)
{
if (product.relatedEntities != null)
{
for (int i = 0; i < product.relatedEntities.Length; i++)
{
if (product.relatedEntities[i].reference.Equals("CONT1234"))
{
if (product.resources != null)
{
foreach (var item in product.resources)
{
if (item.resource != null && item.resource.resourceCharacteristics != null)
{
for (int j = 0; j < item.resource.resourceCharacteristics.Length; j++)
{
var ele = item.resource.resourceCharacteristics;
if (ele[j].name.ToLower().Contains(IMEI_VALUE_NAME))
{
imeiNo = respObj.resourceCharacteristics[0].value = ele[j].value;
break;
}
}
}
break;
}
}
}
}
}
break;
}
}
I want this to be in lambda expression or linq query format. When I try to bind into lambda expression whenever there is null or empty then an error shows up:
instance of an object is not defined
your question is not super clear, but the best query I could get is this:
var query =
(
from product in resourceinfo?.Products ?? Enumerable.Empty<Product>().Take(1)
from relatedEntity in product?.relatedEntities
where relatedEntity.reference.Equals("CONT1234")
from item in product?.resources.Take(1)
from ele in item?.resource?.resourceCharacteristics
where ele.name.ToLower().Contains(IMEI_VALUE_NAME)
select ele.value
).Take(1);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
List<Object> rollerliste = (from row in roller.AsEnumerable() select (row["rolName"])).ToList();
List<Object> yetkiliste = (from row in roller.AsEnumerable() select (row["Visible"])).ToList();
for(int r = 0; r < rollerliste.Count(); r++)
{
for (int y = 0; y < yetkiliste.Count(); y++)
{
if(rollerliste[r].ToString() == "frmMasalar" && yetkiliste[y].ToString() == "true" && r == y)
{
cu.frmMasalar = 1;
}
else
{
cu.frmMasalar = 0;
}
}
}
Actually
if(rollerliste[r].ToString() == "frmMasalar" && yetkiliste[y].ToString() == "true" && r == y)
it seems to be checking for correct data but not working.
rollerliste
yetkiliste
frmMasalar
True
frmYonetim
True
I just want to make check rollerliste if column1 is true "button.Enable = true" or false
Please note that, within your loops, you are overwriting cu.frmMasalar over and over again. That alone might be the reason you're not getting what you want.
I'm not sure I totally understand what you want to do. But, check whether this might be simpler:
cu.frmMasalar = 0;
foreach(var row in roller.AsEnumerable()) {
if((string) row["rolName"] == "frmMasalar" && (bool) row["Visible"]) {
cu.frmMasalar = 1;
break;
}
}
There are also more condensed ways to do this, if the point is to find the one entry where rolName == "frmMasalar":
cu.frmMasalar = 0;
var matchingRow = roller.AsEnumerable()
.FirstOrDefault(r => (string) r["rolName"] == "frmMasalar"
&& (bool) row["Visible"]);
if(matchingRow != null)
cu.frmMasalar = 1;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a problem with my code and says: Compiler Error Message: CS1513: } expected
for (int i = 2001; i<= 2017; i++)
{
for (int j = 1; j <= 12; j++)
{
thursday = LastThursdayOfTheMonth(j, i);
if (LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)))
{
line = LStock.First(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2));
int index = LStock.IndexOf(line) - 1;
LFridays.Add(LStock.ElementAt(index));
}
else
{
while (LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)) != true)
{
if (LStock.Any(element => element.date == thursday.ToString("dd-MMM-yyyy", dtf2)))
{
line = LStock.First(element => element.date == thursday.ToString(("dd-MMM-yyyy"), dtf2));
LFridays.Add(line);
}
else
{
thursday.AddDays(1);
}
}
}
else if (thursday.ToString("dd-MMM-yyyy",dtf2).Contains("25-dic"))
{
friday = LastFridayOfTheMonth(j, i);
bool find = false;
while (find != true )
{
if (LStock.Any(element => element.date == friday.ToString("dd-MMM-yyyy", dtf2)))
{
find = true;
line = LStock.First(element => element.date == friday.ToString("dd-MMM-yyyy", dtf2));
LFridays.Add(line);
}
else
{
friday = friday.AddDays(1);
}
}
}
}
}
I supposed that I have a problem with {} but it's impossible to find where it is. Can someone find if i have less {} or is a different error?
Thank u so much
Are we really supposed to have a
if()
{
}
else
{
}
else if()
{
}
Shouldn't that be
if()
{
}
else if()
{
}
else
{
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have to read integers from console sepetating with white characters until end of file, but I don't know how, I search for the answer but I couldn't find it.
while((x = Console.Read()) != null)
Note that the method does not return -1 unless you perform one of the
following actions:
Simultaneously press the Control modifier key and Z console key (Ctrl+Z), which signals the end-of-file condition.
Press an equivalent key that signals the end-of-file condition, such as the F6 function key in Windows.
Redirect the input stream to a source, such as a text file, that has an actual end-of-file character.
MSDN Read() method.
Then you can read file by character and calculate each separated value with simple math. It's lazy then will not iterate file to the end immediately for calculating all values.
static void Main(string[] args)
{
foreach (int i in Read(Console.In))
{
Console.WriteLine(i);
}
}
static IEnumerable<int> Read(TextReader rdr)
{
int ch;
bool neg = false;
int value = 0;
int count = 0;
while ((ch = rdr.Read()) != -1)
{
if (char.IsWhiteSpace(ch))
{
if (count > 0)
yield return neg ? -value : value;
count = 0;
value = 0;
neg = false;
}
else if (count == 0 && ch == '-')
{
neg = true;
}
else if (ch >= '0' && ch <= '9')
{
count++;
value = value*10 + (ch - '0');
}
else
throw new InvalidDataException();
}
if (count > 0)
yield return neg ? -value : value;
}
int values are not nullable. You have to use negative one.
while((x = Console.Read()) != -1)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to break two for loop at the highlighted line. (after showing the MessageBox.Show("THE ITEM ID DOES NOT EXIST.!"); )
bool conditionitem = true;
for (int cun = 0; cun < ItemIdNumber.Length; cun++)
{
int Item_Id = Convert.ToInt32(ItemIdNumber[cun]);
for (int yyu = 0; yyu <= 1258038; yyu++)
{
int weer = c[yyu];
if (weer == Item_Id)
{
conditionitem = false;
itemseq = yyu;
}
}
if (conditionitem != false)
{
MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
break; //--> here i want two break for two times
}
}
By this break it only break the first loop.
Two options I can think of:
(1) Set a flag inside the second loop before you break out of it. Follow the inner iteration with a condition that breaks out of the first iteration if the flag is set.
bool flag = false;
foreach (item in Items)
{
foreach (item2 in Items2)
{
flag = true; // whenever you want to break
break;
}
if (flag) break;
}
(2) Use a goto statement.
foreach (item in Items)
{
foreach (item2 in Items2)
{
goto GetMeOutOfHere: // when you want to break out of both
}
}
GetMeOutOfHere:
// do what you want to do.
You can refactor the loop to be a method that finds the item:
SomeType SomeMethod(int itemId)
{
for (int cun = 0; cun < ItemIdNumber.Length; cun++)
{
int Item_Id = Convert.ToInt32(ItemIdNumber[cun]);
for (int yyu = 0; yyu <= 1258038; yyu++)
{
if (c[yyu] == itemId) return yyu;
}
}
return null;
}
Then just use that:
var item = SomeMethod(Item_Id);
if(item == null)
{
MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
}
else
{
// ...
}
This also avoids mixing UI logic and internal logic.
Put your nested loop into a function and return true/false whenever you want to break the loop?
bool Function()
{
for(int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10; ++j)
{
if (error)
{
MessageBox.Show("THE ITEM ID DOES NOT EXIST.!");
return false;
}
}
}
return true;
}
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Following is my code,where I need to continue the loop in case of exception inside try block.
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
try
{
if (name != "")
{
name=name.ToString().Substring(12);
break;
}
}
catch{
continue;
}
}
Please tell me if i`m wrong at any place in my code.Please check for performance wise too.
Thanks in advance.
The continue is unneeded. It will automatically continue.
var name = doc.FirstOrDefault(x => !string.IsNullOrEmpty(x) && x.Length >= 12);
You don't need exception handling if you can avoid it:
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
if(name != null && name.Length >= 12)
{
name = name.Substring(12);
break;
}
}
Never use exceptions for something that is not exceptional. If you don't expect any of documents will have length less that 12, then you can use exceptions (but also not just for control flow):
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
if (name.Length < 12)
throw new FooException("Wrong document found!");
// do something with name
}
You do not need break or continue here.
try this.
for (int i = 0; i < doc.Length; i++)
{
name = doc[i].ToString();
try
{
if (!string.IsNotNullOrEmpty(name))
{
name=name.ToString().Substring(12);
}
}
catch{ }
}