I have 3 tables(in origin its 6 but shorter better here)
they all have have a key column in common I want to filter and union all these tables using linq
Here what I did so far, actually it works but you will understand me why I post this since you saw the code below
var query = (from icr in ictetkikRadyoloji
where icr.CommanId == userId
select new MyCustomModel
{
ID = icr.ID,
IstemTarihi = icr.IstemTarihi,
Laboratuvar = icr.Laboratuvar,
Sonuc = icr.Sonuc,
HastaGuid = "-",
Type_ = "1"
}).Union(from icb in ictetkikBakteriyoloji
where icb.CommanId == userId select new MyCustomModel
{
ID = icb.ID,
IstemTarihi = icb.IstemTarihi,
Laboratuvar = icb.Laboratuvar,
Sonuc = "-",
HastaGuid ="someGuid",
Type_ = "2"
}).Union(from icbk in ictetkikBiyokimya
where icbk.CommanId == userId select new MyCustomModel
{
ID = icbk.ID,
IstemTarihi = DateTime.Now,
Laboratuvar = icbk.Laboratuvar,
Sonuc = "-",
HastaGuid ="SomeGuid",
Type_ = "3"
});
You see its so ugly and its uglier than this in origin :( how can I make this little bit more beauty, since they all return entities as specific Model(MyCustomModel)
I feel like it can be..
You could first perform projection and then union over all cases. The only additional data that projection should have is CommanId. You can use anonymous types, so you don't have to create another class just for intermediate projection. After that you can specify your filter just once and do final projection to MyCustomModel.
For example:
var unionQuery = (from icr in ictetkikRadyoloji
select new
{
ID = icr.ID,
IstemTarihi = icr.IstemTarihi,
Laboratuvar = icr.Laboratuvar,
Sonuc = icr.Sonuc,
HastaGuid = "-",
Type_ = "1",
CommanId = icr.CommanId
}).Union(
(from icb in ictetkikBakteriyoloji
select new
{
ID = icb.ID,
IstemTarihi = icb.IstemTarihi,
Laboratuvar = icb.Laboratuvar,
Sonuc = "-",
HastaGuid = "someGuid",
Type_ = "2",
CommanId = icb.CommanId
})).Union(
(from icbk in ictetkikBiyokimya
select new
{
ID = icbk.ID,
IstemTarihi = DateTime.Now,
Laboratuvar = icbk.Laboratuvar,
Sonuc = "-",
HastaGuid = "SomeGuid",
Type_ = "3",
CommanId = icbk.CommanId
}));
var query = from i in unionQuery
where i.CommanId = userId
select new MyCustomModel
{
ID = i.ID,
IstemTarihi = i.IstemTarihi,
Laboratuvar = i.Laboratuvar,
Sonuc = "-",
HastaGuid = "SomeGuid",
Type_ = "3",
};
Alternatively you could add property CommanId to MyCustomModel and then you don't need final projection and anonymous types.
You have two options:
1. Get List of Lists
var query = (from icr in ictetkikRadyoloji
where icr.CommanId == userId
select new List<MyCustomModel>()
{
new MyCustomModel()
{
ID = icr.ID,
IstemTarihi = icr.IstemTarihi,
Laboratuvar = icr.Laboratuvar,
Sonuc = icr.Sonuc,
HastaGuid = "-",
Type_ = "1"
},
new MyCustomModel
{
ID = icb.ID,
IstemTarihi = icb.IstemTarihi,
Laboratuvar = icb.Laboratuvar,
Sonuc = "-",
HastaGuid = "someGuid",
Type_ = "2"
},
new MyCustomModel
{
ID = icbk.ID,
IstemTarihi = DateTime.Now,
Laboratuvar = icbk.Laboratuvar,
Sonuc = "-",
HastaGuid = "SomeGuid",
Type_ = "3"
}
});
2. Use loop through Objects
Load objects to memory and make 3 different copies of each object.
var result = new List<MyCustomModel>();
var query = (from icr in ictetkikRadyoloji
where icr.CommanId == userId
select icr)
.ToList()
.ForEach(x =>
{
result.Add(new MyCustomModel()
{
ID = icr.ID,
IstemTarihi = icr.IstemTarihi,
Laboratuvar = icr.Laboratuvar,
Sonuc = icr.Sonuc,
HastaGuid = "-",
Type_ = "1"
});
result.Add(new MyCustomModel()
{
ID = icr.ID,
IstemTarihi = icr.IstemTarihi,
Laboratuvar = icr.Laboratuvar,
Sonuc = "-",
HastaGuid = "someGuid",
Type_ = "2"
});
result.Add(new MyCustomModel()
{
ID = icr.ID,
IstemTarihi = DateTime.Now,
Laboratuvar = icr.Laboratuvar,
Sonuc = "-",
HastaGuid = "SomeGuid",
Type_ = "3"
});
});
Related
I have a dictionary. When I try to access that dictionary, it is returning a null value when it should definately be returning an actual value. I'm stumped. Any help is always appreciated.
if let d = response.result.value {
print(d) //prints correct data
let prices = d["prices"] as? [[String:AnyObject?]]
print(prices) //prints nil
let best_price = prices?[0]
let price = best_price?["price"] as? String
print(price) //prints nil
}
Console:
{
item = {
"item_number" = 57;
image = "http://example.com/tent.jpg";
name = "Small Red Tent";
};
prices = (
{
link = "http://example.com/id=19";
price = "58.15";
rating = "3.64";
"vendor_id" = 50;
},
{
link = "http://example.com/id=50";
price = "58.14";
rating = "5.00";
"vendor_id" = 110;
},
{
link = "http://example.com/id=26";
price = "50.40";
rating = "4.71";
"vendor_id" = 73;
},
{
link = "http://example.com/id=12";
price = "47.16";
rating = "4.00";
"vendor_id" = 1;
},
{
link = "http://example.com/id=13";
price = "45.75";
rating = "3.90";
"vendor_id" = 25;
},
{
link = "http://example.com/id=16";
price = "41.32";
rating = "3.02";
"vendor_id" = 16;
},
{
link = "http://example.com/id=1";
price = "36.59";
rating = "4.84";
"vendor_id" = 51;
},
{
link = "http://example.com/id=2";
price = "36.29";
rating = "3.26";
"vendor_id" = 43;
},
{
link = "http://example.com/id=13";
price = "34.59";
rating = "4.14";
"vendor_id" = 48;
},
{
link = "http://example.com/id=3";
price = "32.00";
rating = "4.29";
"vendor_id" = 53;
},
{
link = "http://example.com/id=4";
price = "24.50";
rating = "4.16";
"vendor_id" = 8;
},
{
link = "http://example.com/id=5";
price = "15.00";
rating = "4.87";
"vendor_id" = 39;
},
{
link = "http://example.com/id=6";
price = "0.00";
rating = "3.00";
"vendor_id" = 65;
}
);
}
nil
nil
So the solution was proposed by Larme solved the problem.
let prices = d["prices"] as? [AnyObject]
Although I never figured out why casting d["prices"] as? [[String:AnyObject?]] wouldn't also work.
I am using firebase and I am trying to retrieve data from Firebase Database which is working well however when I call "postDict" I get.
["-KPZAOg58kUdqwYMzlDJ":
{
description = jpeg;
listingImageURL = "https://firebasestorage.googleapis.com/v0/b/hype-central.appspot.com/o/lidsafstings%2FC73FdfB8A0-968df7-44DB-B96B-A10DC1E3A2B7.png?alt=media&token=b6e296f2-8854-4b76-9bfc-470dfd69d11f2f";
location = jpeg;
price = 54;
title = jpeg;
userDisplayName = "Brandon YOIO";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
},
"-KPZ0yLhcfLvP9YWNjbC": {
description = "Description ofvaroe";
listingImageURL = "https://firebadfasestorage.googleapis.com/v0/b/hype-central.appspot.com/o/listings%2F0ACdasfCD645-2E2C-4936-B3DF-37EC55AA0157.png?alt=media&token=b321dasff42f-75b5-4eb3-b129-b0a902cc5926";
location = Vancouver;
price = 34;
title = eclipse;
userDisplayName = "Brandon YOIO";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
},
"-KPoCsNS63JZdbTJIdaP": {
description = Brandon;
listingImageURL = "https://firebadasfsestorage.googleapis.com/v0/b/hype-central.appspot.com/o/listings%2FOptional(%22pRuwvL7WyzQpY0G22ZLYCmTTemB3%22)%2F3EACE28E-D28A-464A-B6D0-FB502E9dafsB4775.jpeg?alt=media&token=1d45bc7b-7273-4d33-8ccd-755f3102a23c";
location = BRANDON;
price = BRANDON;
title = BRANDON;
userDisplayName = "Brandon YOIO";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
},
"-KP_Y7ug7hwrHtW6VNqV": {
description = "Description ofvaroe";
listingImageURL = "https://firebaseddasfstorage.googleapis.com/v0/b/hype-central.appspot.com/o/listings%2FC5daf7991BA-D7E0-4C8A-96E7-0DB17EEBABD7.jpeg?alt=media&token=efd03b4d-6964-4685-9b48-5ed36a4ceb59";
location = "";
price = "";
title = "";
userDisplayName = "Brandon Mayhew";
userid = pRuwvL7WyzQpY0G22ZLYCmTTemB3;
}]
If I call "postDict["-KPZAOg58kUdqwYMzlDJ"]["title"]" It will print the title which is exactly what I want however how do I get an array of all the titles in this JSON format (not just the title of "-KPZAOg58kUdqwYMzlDJ").
How do I get all the title's?
Use :-
rootRef.observeEventType(.Value, withBlock: {(postDictionary) in
if let postDict = postDictionary.value as? [String:AnyObject]{
for each in postDict as [String:AnyObject]{
let autoID = each.0
rootRef.child(autoID).observeEventType(.Value, withBlock: {(specificPost) in
//RETRIEVE YOUR DATA
})
}
}
})
PS:- I have used rootRef as you have not specified your parent nodes in your JSON structure :)
I am using typeahead to add autosuggest functions to a wordpress searchform. As a result I expect two columns, column 1 -> Posttype Post, column 2 -> Posttype page seperation like a hr, followd by posttype media.
I am using three datasets (for every posttype a unique dataset), so frontend renders 3 "tt-dataset". As I am using foundation I would need to add a div class="row"before the datasets, add a "large-6 column" after the "tt-dataset" class and add a closing after every "tt-dataset" as well a a closing div for the row.
I think I could add these classes using JS, but I just doesn't feels right. is there any out-of-the-box solution I am missing? Thank you guys!
My Code up to now, it's hardcoded as I am prototyping right now.
<script type="text/javascript" src="typeahead.bundle.js"></script>
<script type="text/javascript">
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var posts = new Array();
posts[0] = new Object();
posts[0]["name"] = "test Name";
posts[0]["url"] = "http://www.google.com";
posts[0]["image"] = "https://placeholdit.imgix.net/~text?txtsize=19&txt=the_post_thumbnail%28%29&w=450&h=250&txttrack=0";
posts[0]["tokens"] = "Post", "Thumbnail", "test";
posts[1] = new Object();
posts[1]["name"] = "test Name2";
posts[1]["url"] = "http://www.bing.com";
posts[1]["image"] = "https://placeholdit.imgix.net/~text?txtsize=19&txt=the_post_thumbnail%28%29&w=450&h=250&txttrack=0";
posts[1]["tokens"] = "Post", "Thumbnail", "test";
posts[2] = new Object();
posts[2]["name"] = "test Name3";
posts[2]["url"] = "http://www.yahoo.com";
posts[2]["image"] = "https://placeholdit.imgix.net/~text?txtsize=19&txt=the_post_thumbnail%28%29&w=450&h=250&txttrack=0";
posts[2]["tokens"] = "Post", "Thumbnail", "test";
posts[3] = new Object();
posts[3]["name"] = "test Name4";
posts[3]["url"] = "http://www.google.com";
posts[3]["image"] = "https://placeholdit.imgix.net/~text?txtsize=19&txt=the_post_thumbnail%28%29&w=450&h=250&txttrack=0";
posts[3]["tokens"] = "Post", "Thumbnail", "test";
posts[4] = new Object();
posts[4]["name"] = "test Name5";
posts[4]["url"] = "http://www.bing.com";
posts[4]["image"] = "https://placeholdit.imgix.net/~text?txtsize=19&txt=the_post_thumbnail%28%29&w=450&h=250&txttrack=0";
posts[4]["tokens"] = ['Post', 'Thumbnail', 'test'];
posts[5] = new Object();
posts[5]["name"] = "test Name6";
posts[5]["url"] = "http://www.yahoo.com";
posts[5]["image"] = "https://placeholdit.imgix.net/~text?txtsize=19&txt=the_post_thumbnail%28%29&w=450&h=250&txttrack=0";
posts[5]["tokens"] = ['Post', 'Thumbnail', 'test'];
console.log(posts);
var pages = new Array();
pages[0] = new Object();
pages[0]["name"] = "Page 1";
pages[0]["url"] = "http://www.google.com";
pages[0]["tokens"] = "Impressum", "Imprint";
pages[1] = new Object();
pages[1]["name"] = "Page 2";
pages[1]["url"] = "http://www.bing.com";
pages[1]["tokens"] = "Datenschutz";
pages[2] = new Object();
pages[2]["name"] = "Page 3";
pages[2]["url"] = "http://www.yahoo.com";
pages[2]["tokens"] = "AGB";
pages[3] = new Object();
pages[3]["name"] = "Page 4";
pages[3]["url"] = "http://www.google.com";
pages[3]["tokens"] = "Kontakt", "contact";
var cpts = new Array();
cpts[0] = new Object();
cpts[0]["name"] = "CPT 1";
cpts[0]["url"] = "http://www.google.com";
cpts[0]["tokens"] = "John", "Doe";
cpts[1] = new Object();
cpts[1]["name"] = "CPT 2";
cpts[1]["url"] = "http://www.bing.com";
cpts[1]["tokens"] = "Jane", "Doe";
cpts[2] = new Object();
cpts[2]["name"] = "CPT 3";
cpts[2]["url"] = "http://www.yahoo.com";
cpts[2]["tokens"] = "Max", "Muster";
cpts[3] = new Object();
cpts[3]["name"] = "CPT 4";
cpts[3]["url"] = "http://www.google.com";
cpts[3]["tokens"] = "Marianne", "Muster";
$('.typeahead').typeahead({
hint: true,
highlight: "any",
minLength: 0,
maxItem: 15,
maxItemPerGroup: 2,
searchOnFocus: true,
matcher: function () { return true; },
},
{
name: 'posts',
source: substringMatcher(posts),
display: ['tokens','name','url'],
templates: {
empty: [
'<div class="empty-message">',
'Zu Ihrer Suchanfrage konnten leider keine Treffer gefunden werden.',
'</div>'
].join('\n'),
footer : [
'<div class="see-all-results">',
'Alle Ergebnisse <i class="fa fa-chevron-right"></i>',
'<div>'
].join('\n'),
header : [
'<strong>Beiträge</strong>'
].join('\n'),
suggestion: function(data) {
return '<div class="headline-pic" style="background-image: url("' + data.image + '"); background-size: cover;">' + data.name + '</div>';
}
}
},
{
name: 'pages',
source: substringMatcher(pages),
templates: {
header : [
'<strong>Seiten</strong>'
].join('\n'),
suggestion: function(data) {
return '' + data.name + '<br />';
}
}
}
{
name: 'customs',
source: substringMatcher(cpts),
templates: {
header : [
'<strong>Mitarbeiter</strong>'
].join('\n'),
suggestion: function(data) {
return '' + data.name + '<br />';
}
}
});
</script>
I get the following error when I add more than one entity to a dbcontext in EF6.0, if i only add the first, it saves perfectly, if i add a second one then I get the error.
Error:
"Referential integrity constraint violation. A Dependent Role has multiple principals with different values."
Code
using (var context = new ListingLocatorContext())
{
var listing1 = new Listing
{
UserID = 1,
ListingDate = DateTime.Now,
ExpiryDate = DateTime.Now,
Address = string.Empty,
PostalCode = string.Empty,
IsApproved = true,
CityID = 71,
IsTop = true,
IsActive = true,
ViewCount = 0
};
listing1.ListingTypes.Add(new ListingType
{
TypeID = 4
});
var listing2 = new Listing
{
UserID = 1,
ListingDate = DateTime.Now,
ExpiryDate = DateTime.Now,
Address = string.Empty,
PostalCode = string.Empty,
IsApproved = true,
CityID = 71,
IsTop = true,
IsActive = true,
ViewCount = 0
};
listing2.ListingTypes.Add(new ListingType
{
TypeID = 5
});
context.Listings.Add(listing1);
context.Listings.Add(listing2);
context.SaveChanges();
}
DB Diagram
Try replacing those two statements:
listing1.ListingTypes.Add(new ListingType {
TypeID = 4
});
and
listing2.ListingTypes.Add(new ListingType {
TypeID = 5
});
with
var listingType1 = new ListingType {
TypeID = 4,
Listing = listing1
};
var listingType2 = new ListingType {
TypeID = 5,
Listing = listing2
};
context.ListingTypes.Add(listingType1);
context.ListingTypes.Add(listingType2);
{
education = (
{
school = {
id = 108102169223234;
name = psss;
};
type = College;
year = {
id = 142833822398097;
name = 2010;
};
}
);
email = "amvijaycse#gmail.com";
"first_name" = Vijay;
gender = male;
id = 100000782204693;
"last_name" = Kumar;
link = "http://www.facebook.com/profile.php?id=100000782204693";
locale = "en_US";
location = {
id = 106377336067638;
name = "Bangalore, India";
};
name = "Vijay Kumar";
timezone = "5.5";
"updated_time" = "2010-11-21T07:45:11+0000";
}
i needed email,firstname,lastname.
[EDIT: Removed because it was inaccurate]