Reputation: 6615
i have the following code that i need to modify with an if statement to say that if something that make result this, or else make it that. here is what i have now:
var result = (from fs in ctx.datFiles
where fs.File_ID == fID
select new
{
LastName = fs.datClaim.LastName,
FirstName = fs.datClaim.FirstName,
}).FirstOrDefault();
DetailsModelView dmv = new DetailsModelView
{
ClaimID = result.ClaimID,
LastName = result.LastName,
FirstName = result.FirstName,
};
i want to do something like:
var result =""
if (something)
{
result = (from fs in ctx.datFiles
where fs.File_ID == fID
select new
{
LastName = fs.datClaim.LastName,
FirstName = fs.datClaim.FirstName,
}).FirstOrDefault();
}
else
{
result = (from fs in ctx.datFiles
where fs.File_ID == 5
select new
{
LastName = fs.datClaim.LastName,
FirstName = fs.datClaim.FirstName,
}).FirstOrDefault();
}
DetailsModelView dmv = new DetailsModelView
{
ClaimID = result.ClaimID,
LastName = result.LastName,
FirstName = result.FirstName,
};
but i keep getting the following error: Cannot implicitly convert type 'AnonymousType#1' to 'string' when i try to create "result" outside of the LINQ statement.
What do i need to declare result as to make it work, instead of string?
this was just a simple example, my query gets a lot more complicated on the "else" then just the ID change.
Upvotes: 0
Views: 13558
Reputation: 3665
Change var result = "";
to
var result = new
{
LastName = "",
FirstName = "",
};
Upvotes: -1
Reputation: 27627
Here is a functional program that works:
var foo = new {LastName = "", FirstName=""};;
int bar = 1;
if (bar==1)
{
foo = new {LastName = "test1", FirstName="test2"};
}
else
{
foo = new {LastName = "test3", FirstName="test4"};
}
As you can see I've created a dummy anonymous object so that the var foo
knows what object to make. This should obviously be the same anonymous object as you are creating int he two branches of the if statement.
It should also be noted that in the above code there doesn't seem to be any reason not to use DetailsModelView
directly in the linq rather than an anonymous type. It may be your use case is more complex but since there is a simple one to one mapping you can just create your DetailsModelView
where you currently create the anonymous method. Then result can just be defined as being DetailsModelView
.
Upvotes: 4
Reputation: 704
change
var result = "";
to
var result;
Make sure that when you create it outside the LINQ statement you cast it to
DetailsModelView
Upvotes: -2
Reputation: 707
Here's an article that tells you how to cast to an anonymous type: http://tomasp.net/blog/cannot-return-anonymous-type-from-method.aspx
One of the features of anonymous types in C# is that if they contain the same data, then the type will be reused so you can refer to it later -- in this case allowing you to cast to it.
I'm not sure this is the best solution for you here, but it should work.
Upvotes: 0
Reputation: 17808
You don't need a Linq projection here at all.
Edit - but as Servy pointed out, if this is an ORM call, you probably do want it, which means the projection is needed.
ALso your code will throw a null ref exception if FirstOrDefault
returns null.
int searchId = fId;
if(something)
searchId = fId;
else
searchId = 5;
var result = ctx.datFiles.FirstOrDefault(fs => fs.File_ID == searchId)
.Select(fs=> new
{
Id = fs.datClaim.Id,
LastName = fs.datClaim.LastName,
FirstName = fs.datClaim.FirstName,
});
if(result == null)
throw new Exception(); // if there is no result your code will throw!
DetailsModelView dmv = new DetailsModelView
{
ClaimID = result.Id,
LastName = result.LastName,
FirstName = result.FirstName,
};
Upvotes: 0
Reputation: 36111
As others have said, var result = "";
is the same as string result = "";
so when you try to set the value of it to your linq result it tries to cast it as a string.
Depending on how your actual code is designed, you could either declare a simple type with FirstName, LastName properties rathern than returning an anonymous type. Similarly if you are always going to create an instance of DetailsModelView with the results of the LINQ call you could declare result variable as DetailsModelView result = null;
and in your code return a DetailsModelView rather than an anonymous type. At the end you just need to specify the extra properties in DetailsViewModel (if any) rather than instantiate a new one.
DetailsViewModel result = null;
if (something)
result = ;// LINQ Query
else
result = ;// LINQ Query
Upvotes: 1
Reputation: 33391
After var result =""
result already is string.
What about:
int id;
if(something)
{
id = fID;
}
else
{
id = 5;
}
var result = (from fs in ctx.datFiles
where fs.File_ID == id
select new
{
LastName = fs.datClaim.LastName,
FirstName = fs.datClaim.FirstName,
}).FirstOrDefault();
Upvotes: 1