C# 6.0 List that is getting syntax Error , ' , ' - c#

I am parsing a webpage, the issue I am coming across is that ["www.abc.com/"] and ["www.def.com/"] = new List() is getting a "syntax error,',' expected." I made a class that set property for the HtmlTarget, public string Id { get; set; } public Action<HtmlElement> Action { get; set; }, anybody have any suggestions ?
public void Spider(string sURL, HtmlDocument doc, int choice)
{
var urlTargets = new Dictionary<string, List<HtmlTarget>>
{
["www.abc.com/"] = new List<HtmlTarget>()
{
new HtmlTarget
{
Id = "search",
Action = e => e.SetAttribute("value", m_sMfgPartNbr)
},
new HtmlTarget
{
Id = "submit",
Action = e => e.InvokeMember("Click")
}
},
["www.def.com/"] = new List<HtmlTarget>()
{
new HtmlTarget
{
Id = "part",
Action = e => e.SetAttribute("value", m_sMfgPartNbr)
},
new HtmlTarget
{
Id = "submit",
Action = e => e.InvokeMember("Click")
}
}
};
List<HtmlTargets> targets = null;
if (urlTargets.TryGetValue(url, out targets))
{
var inputs = doc.Body.GetElementsByTagName("input");
foreach (HtmlElement element in inputs)
{
var id = element.GetAttribute("id");
foreach(var target in targets)
{
if (target.Id == id)
target.Action(element);
}
}
}
}

The correct syntax prior to C# 6 would be
var urlTargets = new Dictionary<string, List<HtmlTarget>>
{
{ "www.eciaauthorized.com/search", new List<HtmlTarget>()
...
},
{ "www.findchips.com/search", new List<HtmlTarget>()
...
}
}

Related

Filter an embeded list in mongo DB with buildr in C#

I have a problem in writing my query with C#.
Hee is my data model:
public class SpamEntity:MongoEntity
{
public IList<MessageData> MessageData { get; set; }
}
public class MessageData
{
public IList<string> EmbeddedLinks { get; set; }
}
here I have a list of links like :
var myLinks = {"a.com", "b.com", "c.com"}
I want to filter those documents that their list of EmbededLinks is not empty (count or length is zero) and exactly the same as myLinks.
I am halfway and I do not know what to do in continue.
my filter is something like:
var filter =
Builders<SpamEntity>.Filter.ElemMatch(s => s.MessageData,
s => s.EmbeddedLinks != null && s.EmbededLinks == ???);
I think the below should not be correct.
s.EmbededLinks == myLinks
and also I can not use count:
s.EmbeddedLinks.count => It does not work
Could anyone help me with that?
Not sure about mongodb specific filters..
below is a linq variant (assumption ordering doesnt matter!)
var filtered =
spamEntities.MessageData.Where(
m =>
m.EmbeddedLinks.Any() && //ensure EmbeddedLinks has some values
m.EmbeddedLinks.Count() == myLinks.Count() && //and that count is same as our reference collection
m.EmbeddedLinks.Intersect(myLinks).Count() == myLinks.Count()); //ensure elements match
working code
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace SOProject
{
public class SpamEntity
{
public IList<MessageData> MessageData { get; set; }
}
public class MessageData
{
public IList<string> EmbeddedLinks { get; set; }
}
public class SO
{
[Fact]
public void Q_63081601()
{
var myLinks = new List<string> { "a.com", "b.com", "c.com" };
var size = myLinks.Count();
var data2 = new List<string>(myLinks);
var data3 = new List<string>(myLinks) { "sdsadsad.com" };
var data4 = new List<string> { "c.com", "b.com", "a.com" };
var spamEntities = new SpamEntity()
{
MessageData = new List<MessageData>
{
new MessageData()
{
EmbeddedLinks = new List<string> { "", "aaaa.com", "bbb.com" }
},
new MessageData()
{
EmbeddedLinks = data2
},
new MessageData
{
EmbeddedLinks = Enumerable.Empty<string>().ToList()
},
new MessageData()
{
EmbeddedLinks = data2
},
new MessageData()
{
EmbeddedLinks = data3
},
new MessageData()
{
EmbeddedLinks = data4
}
}
};
var filtered =
spamEntities.MessageData.Where(
m =>
m.EmbeddedLinks.Any() && //ensure EmbeddedLinks has some values
m.EmbeddedLinks.Count() == myLinks.Count() && //and that count is same as our reference collection
m.EmbeddedLinks.Intersect(myLinks).Count() == myLinks.Count()); //ensure elements match
Assert.True(filtered.Any());
Assert.Equal(3, filtered.Count());
}
}
}

How can I use LINQ to select a property inside of an IEnumerable that's inside another IEnumerable?

I have this LINQ Query which returns the data shown in the image below:
var result = jmdict
.Where(x => x.Sequence == 1438690)
.SelectMany(entry =>
entry.Senses.Select(s =>
(entry.Sequence, s.Glosses.Where(x => x.Language.Code == "eng"))));
Does anyone have any ideas how I could modify this so that item2 would return just the value of Term ("weather","the elements" etc) instead of the IEnumerator which it currently returns.
1) I tried this suggestion which was given to me earlier
var r3 =
jmdict.Where(x => x.Sequence == 1438690)
.SelectMany(entry => entry.Senses.Select(s => s.Glosses.Where(x => x.Language.Code == "eng")
.Select(y => (entry.Sequence, y => y.Term))));
But this does not work and there's an error line under the last Select giving this error message :
GetDetails.cs(155,155): Error CS0411: The type arguments for method
'Enumerable.Select(IEnumerable,
Func)' cannot be inferred from the usage. Try
specifying the type arguments explicitly. (CS0411) (Download)
public static List<IJapaneseDictionaryEntry> jmdict;
public class JapaneseDictionaryEntry : IJapaneseDictionaryEntry {
public int Sequence { get; set; }
private readonly List<Sense> senses = new List<Sense>();
public IEnumerable<ISense> Senses => this.senses;
}
public interface ISense {
IEnumerable<Gloss> Glosses { get; }
}
public class Gloss {
public string Term { get; }
public Language Language { get; }
public Gloss(string term, Language language, string gender)
{
this.Term = term;
this.Language = language;
this.Gender = gender;
}
}
Finally Here is your corrected second query
var r3 = jmdict.Where(x => x.Sequence == 1438690)
.SelectMany(entry => entry.Senses.SelectMany(s => s.Glosses.Where(x => x.Language.Code == "eng")
.Select(y => (entry.Sequence, y => y.Term))));
I have taken your schema and modified a bit, here out put will be
{Sequence= ?, Term=?}
data.Where(a=>a.Sequence==2)
.SelectMany(b=>b.Senses.SelectMany(x=>x.Glosses.Where(g => g.Language == "German"))
.Select(y => new {b.Sequence, y.Term}));
public class JapaneseDictionaryEntry
{
private List<Sense> senses = new List<Sense>();
public int Sequence { get; set; }
public List<Sense> Senses { get { return senses; } set { senses = value; } }
}
public class Sense
{
private List<Gloss> glosses = new List<Gloss>();
public List<Gloss> Glosses { get { return glosses; } set { glosses = value; } }
}
public class Gloss
{
public string Term { get; set; }
public string Language { get; set; }
}
class Program
{
static List<JapaneseDictionaryEntry> GetData()
{
return new List<JapaneseDictionaryEntry>() {
new JapaneseDictionaryEntry()
{
Sequence = 1,
Senses = new List<Sense>() { new Sense() { Glosses = new List<Gloss>() {
new Gloss() { Term = "1",Language="English"},
new Gloss() { Term = "2",Language="German" },
new Gloss() { Term = "3",Language="German" },
new Gloss() { Term = "4",Language="English" }
}
} }
},
new JapaneseDictionaryEntry()
{
Sequence = 2,
Senses = new List<Sense>() { new Sense() { Glosses = new List<Gloss>() {
new Gloss() { Term = "a", Language="English"},
new Gloss() { Term = "b", Language="German" },
new Gloss() { Term = "c", Language="German" },
new Gloss() { Term = "d", Language="English"}
}
} }
}
};
}
static void Main(string[] args)
{
var data = GetData();
var termData = data.Where(a=>a.Sequence==2)
.SelectMany(b=>b.Senses.SelectMany(x=>x.Glosses.Where(g => g.Language == "German"))
.Select(y => new {b.Sequence, y.Term}));
foreach (var item in termData)
{
Console.WriteLine(item);
}
}}

How to convert a list to JSON in C#?

I have a City_State list:
City_State[0].Range="\"city\":\"REDMOND\",\"state\":\"AK\"";
City_State[1].Range="\"city\":\"Alex City\",\"state\":\"
How to convert it into json like below:
var _pairs = new
{
criteria = new { cities = new[] { new { city = "REDMOND", state = "WA" },
new { city = "Alex City", state = "AL" } }
} ;
I tried the code below, but it does not work:
var _pairs = new { criteria = new { cities = new[] { _paged_City_State.ToArray() } } };
If you had these classes:
public class CityStateRaw
{
public string Range { get; set; }
}
public class CityState
{
public string City { get; set; }
public string State { get; set; }
}
The following code would work:
var ranges = new[]
{
new CityStateRaw { Range = "{\"city\":\"REDMOND\",\"state\":\"AK\"}" },
new CityStateRaw { Range = "{\"city\":\"Alex City\",\"state\":\"foo\"}" },
};
var list = ranges
.Select(raw => JsonConvert.DeserializeObject<CityState>(raw.Range))
.ToList();
But if this doesn't match your expectations you should be more concrete about what your exact input and the expected output should be.

Set PredicateBuilder also on child collection

I am trying to apply the predicate not only to the entity parent but also to the child collection. The following is part of my code:
var predicate = PredicateBuilder.New<Entity>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or(p => p.Name.Equals(temp));
predicate = predicate.Or(p => p.Addresses.Select(x=> x.Name.Equals(temp));
}
The line predicate = predicate.Or(p => p.Addresses.Select(x=> x.Name.Equals(temp)); is not working ? any ideas as to why?
EDIT
In the following demo I am asking to get entity parent and child with name = tom but I also get child name = tom2.
private static void Main(string[] args)
{
var result = GetAll(GetAll(), new List<string> { "tom" });
}
private static List<User> GetAll()
{
return new List<User>
{
new User
{
Id = 1,
Name = "tom",
Addesses = new List<Addesses>
{
new Addesses { Id = 1, Name = "tom" },
new Addesses { Id = 1, Name = "tom2" },
}
},
new User
{
Id = 1,
Name = "sam",
Addesses = new List<Addesses>
{
new Addesses { Id = 1, Name = "sam" },
new Addesses { Id = 1, Name = "sam2" },
}
},
};
}
private static List<User> GetAll(List<User> users, List<string> keywords)
{
var predicate = PredicateBuilder.New<User>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or(p => p.Name.Equals(temp));
predicate = predicate.Or(p => p.Addesses.Any(x => x.Name.Equals(temp)));
}
var result = users
.Where(predicate)
.ToList();
return result;
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public List<Addesses> Addesses { get; set; }
}
public class Addesses
{
public int Id { get; set; }
public string Name { get; set; }
}
The call to p.Addresses.Select(x=> x.Name.Equals(temp) is not returning a boolean result.
Depending on your actual logic you may want to look into Any:
predicate = predicate.Or(p => p.Addresses.Any(x=> x.Name.Equals(temp));
or All:
predicate = predicate.Or(p => p.Addresses.All(x=> x.Name.Equals(temp));

Why do I keep getting an error on converting a generic list to the same list type?

I am running into this error
cannot convert from 'System.Collections.Generic.List' to 'HWC.DataAccess.FAREmailList' HWC.DataAccess
I am not understanding this error because its the same list type
here is the method that I am using
public void PrepareToSendEmailFromFAR(int id)
{
HWC = new HWCEntities();
FileAReport far = HWC.FileAReports.Where(w => w.FileAReportID == id).FirstOrDefault();
List<FAREmailList> emailList = null;
if(far.DistrictID != 0)
{
emailList = new List<FAREmailList>();
var query = from dcx in HWC.DistrictContactXREFs
where
dcx.DistrictID == far.DistrictID
select new
{
dcx.ContactID,
dcx.Contact.ContactEmail,
dcx.Contact.ContactName
};
foreach(var a in query)
{
emailList.Add(new FAREmailList
{
ContactName = a.ContactName,
EmailAddress = a.ContactEmail
});
}
SendEmailFromFAR(emailList);
}
if(far.DistrictID == 0)
{
emailList = new List<FAREmailList>();
var query = from dcx in HWC.DistrictContactXREFs
join d in HWC.Districts on dcx.DistrictID equals d.DistrictID into d_join
from d in d_join.DefaultIfEmpty()
join sp in HWC.StateProvinces on new { StateProvinceID = d.StateID } equals new { StateProvinceID = sp.StateProvinceID }
where
d.StateID == far.StateCountyID
select new
{
dcx.ContactID,
dcx.Contact.ContactEmail,
dcx.Contact.ContactName
};
foreach (var a in query)
{
emailList.Add(new FAREmailList
{
ContactName = a.ContactName,
EmailAddress = a.ContactEmail
});
}
SendEmailFromFAR(emailList);
}
}
and here is the method thats receiving the emailList
public void SendEmailFromFAR(FAREmailList el)
{
}
the data class is
public class FAREmailList
{
public string ContactName { get; set; }
public string EmailAddress { get; set; }
}
the error is being thrown at
SendEmailFromFAR(emailList);
I am not seeing what the issue is, is it because this is all in the same class file?
The error makes sense to me. emailList is of type List<FAREmailList> and SendEmailFromFAR takes a FAREmailList as input.

Categories

Resources